美文网首页
开启前台服务

开启前台服务

作者: 无良安生 | 来源:发表于2020-11-20 01:01 被阅读0次

我就是想做个笔记而已,给自己看看而已

第一步:
面向Android 9(API级别28)或更高版本并使用前台服务的应用必须请求 FOREGROUND_SERVICE 权限

so先来个权限
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

然后启动一个前台服务

val pendingIntent: PendingIntent =
Intent(this, ExampleActivity::class.java).let { notificationIntent ->
PendingIntent.getActivity(this, 0, notificationIntent, 0)
}

val notification: Notification = Notification.Builder(this, CHANNEL_DEFAULT_IMPORTANCE)
.setContentTitle(getText(R.string.notification_title))
.setContentText(getText(R.string.notification_message))
.setSmallIcon(R.drawable.icon)
.setContentIntent(pendingIntent)
.setTicker(getText(R.string.ticker_text))
.build()

// Notification ID cannot be 0.
startForeground(ONGOING_NOTIFICATION_ID, notification)

最后你发现,死活就是tm的没开起来,还崩溃了,哈哈啊啊啊哈哈

然后再看看,发现还需要tn的什么通道,先不管了,先上代码:

 val notificationManager: NotificationManager =
        getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val name = getString(R.string.app_name)
        val descriptionText = getString(R.string.app_name)
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val channel = NotificationChannel("1232155654686578343", name, importance).apply {
            description = descriptionText
        }
        // Register the channel with the system

        notificationManager.createNotificationChannel(channel)
    }

就是这个小混蛋创建了什么通道
然后调用
notificationManager.notify(1, notification)
好了差不多了
完整代码就是这样的:
@RequiresApi(Build.VERSION_CODES.O)
fun createNotificationChannel() {
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = getString(R.string.app_name)
val descriptionText = getString(R.string.app_name)
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel("1232155654686578343", name, importance).apply {
description = descriptionText
}
// Register the channel with the system

        notificationManager.createNotificationChannel(channel)
    }
    val pendingIntent: PendingIntent =
        Intent(this, MainActivity::class.java).let { notificationIntent ->
            PendingIntent.getActivity(this, 0, notificationIntent, 0)
        }

    val notification: Notification = Notification.Builder(baseContext, "1232155654686578343")
        .setContentTitle(getText(R.string.app_name))
        .setContentText(getText(R.string.app_name))
        .setSmallIcon(R.drawable.ic_back)
        .setContentIntent(pendingIntent)
        .setTicker(getText(R.string.app_name))
        .build()
    startForeground(123, notification)
    notificationManager.notify(1, notification)
}

然后发现你的前台服务就这么简单愉快的启动起来了,至于能不能保活我就不懂了,等待验证了
反正搞了这么多都是为了保活

注意:Notification.Builder(baseContext, "1232155654686578343")和NotificationChannel("1232155654686578343", name, importance)
这两个是要一样的

其实不懂的看官网,比网上哪些乱七八糟的靠谱多了,百度百度 .... 摆渡摆渡..... 全是一个抄一个.....

相关文章

  • 开启前台服务

    这两天开始做一个音乐播放器,慢慢完善功能,打算把遇到的一些问题记下来,故对代码没有什么解释。。希望读者能有所收获。...

  • 开启前台服务

    我就是想做个笔记而已,给自己看看而已 第一步:面向Android 9(API级别28)或更高版本并使用前台服务的应...

  • 安卓8.0之后开启服务问题

    1.安卓8.0之后开启服务google规定只能是前台服务,不能后台偷做后台工作,所以需要的是startForegr...

  • Android 服务的限制

    服务的分类 Google官网将Android服务分为了三种,前台服务,后台服务和绑定服务: 前台 前台服务执行一些...

  • 前台服务

    例如点击通知跳转到Three.class页面,点击返回时回退到Second.class,需要添加

  • android前台服务以及通知

    什么是前台服务 前台服务是那些被认为用户知道(用户认可所认可)且在系统内存不足的时候不允许系统杀死的服务。前台服务...

  • 进程保活方案

    方案:1、开启一个像素的Activity2、前台服务3、相互唤醒(广播或系统应用等)4、JobSheduler5、...

  • Android通知栏前台服务

    一、前台服务的简单介绍 前台服务是那些被认为用户知道且在系统内存不足的时候不允许系统杀死的服务。前台服务必须给状态...

  • Android通知栏前台服务

    一、前台服务的简单介绍 前台服务是那些被认为用户知道且在系统内存不足的时候不允许系统杀死的服务。前台服务必须给状态...

  • APP开发实战69-前台服务

    17.4前台服务 前台服务是被认为是用户已知的正在运行的服务,当系统需要释放内存时不会优先杀掉该进程。前台进程必须...

网友评论

      本文标题:开启前台服务

      本文链接:https://www.haomeiwen.com/subject/anvriktx.html