MainActivity:
package com.demo
import android.app.NotificationManager
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.gorhaf.ball.databinding.ActivityMainBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
private val TAG = MainActivity::class.simpleName
private lateinit var binding: ActivityMainBinding
private lateinit var notificationManager: NotificationManager
private var mainCoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
private var updateNotificationJob: Job? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
// Example of a call to a native method
// binding.sampleText.text = stringFromJNI()
// 初始化数据
initData()
// 初始化事件
initEvent()
}
override fun onDestroy() {
super.onDestroy()
}
private fun initData() {
notificationManager = getSystemService(NotificationManager::class.java)
LikeNotificationChannel.create(notificationManager)
TopNotificationChannel.create(notificationManager)
HotNotificationChannel.create(notificationManager)
}
private fun initEvent() {
binding.mainSendBtn.setOnClickListener { send() }
binding.mainCancelBtn.setOnClickListener { cancel() }
binding.mainUpdateBtn.setOnClickListener { update() }
}
private fun send() {
// LikeNotificationChannel.send(notificationManager, this)
TopNotificationChannel.send(notificationManager, this)
}
private fun cancel() {
// LikeNotificationChannel.cancel(notificationManager)
TopNotificationChannel.cancel(notificationManager)
mainCoroutineScope.cancel("取消通知")
}
private fun update() {
// mainCoroutineScope.cancel("取消之前的任务")
Log.i(TAG, "update: ${mainCoroutineScope.isActive}")
if (updateNotificationJob != null && updateNotificationJob!!.isActive) {
updateNotificationJob!!.cancel("取消之前的任务,开启新的任务")
}
if (!mainCoroutineScope.isActive) {
mainCoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
}
updateNotificationJob = mainCoroutineScope.launch {
while (true) {
TopNotificationChannel.update(notificationManager, this@MainActivity)
delay(1000)
}
}
}
/**
* A native method that is implemented by the 'ball' native library,
* which is packaged with this application.
*/
external fun stringFromJNI(): String
companion object {
// Used to load the 'ball' library on application startup.
init {
System.loadLibrary("ball")
}
}
}
LikeNotificationChannel:
package com.demo
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import androidx.core.app.NotificationCompat
import com.demo.music.MusicActivity
object LikeNotificationChannel {
var id: String = "like"
var name: String = "喜欢"
var importance: Int = NotificationManager.IMPORTANCE_HIGH
val notificationChannel: NotificationChannel = NotificationChannel(id, name, importance)
val notifyId: Int = 1
fun create(notificationManager: NotificationManager) {
notificationManager.createNotificationChannel(notificationChannel)
}
fun send(notificationManager: NotificationManager, context: Context) {
val intent = PendingIntent.getActivity(
context,
0,
Intent(context, MusicActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
val notification = NotificationCompat.Builder(context, LikeNotificationChannel.id)
.setContentTitle("This is like content title")
.setContentText("This is like content text")
.setContentInfo("This is like content info")
.setContentIntent(intent)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setLargeIcon(
BitmapFactory.decodeResource(
context.resources,
R.drawable.ic_launcher_background
)
)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setAutoCancel(false)
.setOngoing(true)
.build()
notificationManager.notify(notifyId, notification)
}
fun cancel(notificationManager: NotificationManager) {
notificationManager.cancel(notifyId)
}
}
TopNotificationChannel:
package com.demo
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import androidx.core.app.NotificationCompat
object TopNotificationChannel {
var id: String = "top"
var name: String = "排行榜"
var importance: Int = NotificationManager.IMPORTANCE_HIGH
val notificationChannel: NotificationChannel = NotificationChannel(id, name, importance)
val notifyId: Int = 1
fun create(notificationManager: NotificationManager) {
notificationManager.createNotificationChannel(notificationChannel)
}
fun send(notificationManager: NotificationManager, context: Context) {
val intent = PendingIntent.getActivity(
context,
0,
Intent(context, MainActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
val notification = NotificationCompat.Builder(context, LikeNotificationChannel.id)
.setContentTitle("当前时间")
.setContentText("${System.currentTimeMillis()}")
.setContentInfo("This is like content info")
.setContentIntent(intent)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setLargeIcon(
BitmapFactory.decodeResource(
context.resources,
R.drawable.ic_launcher_background
)
)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setAutoCancel(false)
.setOngoing(true)
.build()
notificationManager.notify(notifyId, notification)
}
fun cancel(notificationManager: NotificationManager) {
notificationManager.cancel(notifyId)
}
fun update(notificationManager: NotificationManager, context: Context) {
val intent = PendingIntent.getActivity(
context,
0,
Intent(context, MainActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
val notification = NotificationCompat.Builder(context, LikeNotificationChannel.id)
.setContentTitle("当前时间")
.setContentText("${System.currentTimeMillis()}")
.setContentInfo("This is like content info")
.setContentIntent(intent)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setLargeIcon(
BitmapFactory.decodeResource(
context.resources,
R.drawable.ic_launcher_background
)
)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setAutoCancel(false)
.setOngoing(true)
.build()
notificationManager.notify(notifyId, notification)
}
}
HotNotificationChannel:
package com.demo
import android.app.NotificationChannel
import android.app.NotificationManager
object HotNotificationChannel {
var id: String = "hot"
var name: String = "热点新闻"
var importance: Int = NotificationManager.IMPORTANCE_HIGH
val notificationChannel: NotificationChannel = NotificationChannel(id, name, importance)
fun create(notificationManager: NotificationManager) {
notificationManager.createNotificationChannel(notificationChannel)
}
}