常驻通知栏的示例


常驻通知栏的示例,用于保活。


    private fun createNotice() {
        val mNotificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        // 通知渠道的id
        val id = "my_channel_02"
        // 用户可以看到的通知渠道的名字.
        val name: CharSequence = getString(R.string.channel_name)
        //用户可以看到的通知渠道的描述
        val description = getString(R.string.channel_description)
        val importance = NotificationManager.IMPORTANCE_HIGH
        val mChannel = NotificationChannel(id, name, importance)
        //配置通知渠道的属性
        mChannel.description = description
        //设置通知出现时的闪灯(如果 android 设备支持的话)
        mChannel.enableLights(false)
        mChannel.lightColor = Color.RED
        //设置通知出现时的震动(如果 android 设备支持的话)
        mChannel.enableVibration(false)
        mChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
        //最后在notificationmanager中创建该通知渠道 //
        mNotificationManager.createNotificationChannel(mChannel)
        val notification: Notification = Notification.Builder(this, id)
            .setContentTitle("Title").setContentText("You've received new messages.")
            .setSmallIcon(R.drawable.ic_launcher)
            .setChannelId(id)
            .build()
        startForeground(2, notification)
    }

声明:HEUE NOTE|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA 4.0协议进行授权

转载:转载请注明原文链接 - 常驻通知栏的示例