【问题标题】:Android Forground sevice stops when the app is closed关闭应用程序时,Android 前台服务停止
【发布时间】:2020-06-09 17:10:10
【问题描述】:

我正在启动一个在后台接收数据的后台服务,所以为此,我使用了 android 前台服务,该服务在某些手机(MI A2 Stock Android)中运行良好,但在我删除应用程序时在某些手机中从后台托盘服务被破坏。

class MyService : Service() {
    private val CHANNEL_ID = "ForegroundService"

    companion object {
        fun stopService(context: Context) {
            val stopIntent = Intent(context, MyService::class.java)
            context.stopService(stopIntent)
        }
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        // I get some data from intent 
        // My code which runs in the background

        createNotificationChannel()
        val notificationIntent = Intent(this, MainActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(
            this,
            0, notificationIntent, 0
        )

        val notification = NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("App is syncing")
            .setContentText("")
            .setPriority(2)
            .setSmallIcon(android.R.drawable.ic_dialog_alert)
            .setContentIntent(pendingIntent)
            .build()
        startForeground(190, notification)
        return START_NOT_STICKY
    }

    override fun onBind(intent: Intent): IBinder? {
        return null
    }

    private fun createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val serviceChannel = NotificationChannel(
                CHANNEL_ID, "Foreground Service Channel",
                NotificationManager.IMPORTANCE_DEFAULT
            )
            val manager = getSystemService(NotificationManager::class.java)
            manager!!.createNotificationChannel(serviceChannel)
        }
    }
}

这就是我启动服务的方式

val serviceIntent = Intent(this, MyService::class.java)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(serviceIntent)
        } else {
            startService(serviceIntent)
        }

所以我的问题是,即使从后台托盘中删除了 APP,我如何才能让我的服务运行。

【问题讨论】:

    标签: android kotlin service background-service


    【解决方案1】:

    做这些事

    1) 通过在应用程序关闭时重写此方法重新启动服务 您的服务类,复制并粘贴此

    override fun onTaskRemoved(rootIntent: Intent?) {
        val restartServiceIntent = Intent(applicationContext, this.javaClass)
        restartServiceIntent.setPackage(packageName)
        val restartServicePendingIntent = PendingIntent.getService(
            applicationContext,
            1,
            restartServiceIntent,
            PendingIntent.FLAG_ONE_SHOT
        )
        val alarmService =
            applicationContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        alarmService[AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000] =
            restartServicePendingIntent
        super.onTaskRemoved(rootIntent)
    }
    

    2) 将此START_NOT_STICKY 更改为START_STICKY

    3) 要求用户从设置中启用自动运行权限,此功能 在mini、vivo、华为、oppo等定制操作系统中提供。

    4)并且您忘记在设备启动时像您一样重新启动服务 当设备需要使用广播接收器来重启服务 重启

    【讨论】:

    • 仍然面临同样的问题。
    • 要连续运行服务应用程序杀死你必须重新启动服务,没有其他选择
    • 所以执行我提到的步骤
    • 我已按照 1 和 2 步骤进行操作。什么是第三步,我希望第四步是可选的
    • 3 步 - 转到设置并在服务停止的设备中启用自动运行
    猜你喜欢
    • 1970-01-01
    • 2013-05-15
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多