【问题标题】:Foreground service content intent not resuming the app but relaunching it前台服务内容意图不是恢复应用程序而是重新启动它
【发布时间】:2020-04-26 03:41:32
【问题描述】:

我一直在浏览许多关于从前台服务恢复活动的主题,但没有找到任何具体的问题答案。

我正在尝试在我的应用程序中放置一个前台服务,并且我希望在单击服务通知而不是重新启动它时恢复应用程序。我已经尝试使用来自PackageManagergetLaunchIntentForPackage() 方法,这是最接近我想要做的。

但是,当通过点击通知恢复应用时,Activity 的onCreate 仍然被调用。

所以这是我的问题,如何从通知的内容意图恢复应用程序?

我在活动的onStop 中启动我的 ForegroundService,所以当应用程序被杀死或发送到后台时它会被调用。

override fun onStop() {
    super.onStop()
    Log.v(TAG, "onStop")
    ForegroundService.startService(this, "Hellooooooo, here is the background")
}

前台服务

class ForegroundService: Service() {

    companion object {
        private const val CHANNEL_ID = "ForegroundServiceChannel"

        fun startService(context: Context, message: String) {
            val startIntent = Intent(context, ForegroundService::class.java)
            startIntent.putExtra("inputExtra", message)
            ContextCompat.startForegroundService(context, startIntent)
        }
        fun stopService(context: Context) {
            val stopIntent = Intent(context, ForegroundService::class.java)
            context.stopService(stopIntent)
        }
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        val input = intent!!.getStringExtra("inputExtra")

        val launchIntent = packageManager.getLaunchIntentForPackage(APP_PACKAGE)
        val contentIntent = PendingIntent.getActivity(applicationContext, 0,
            launchIntent, 0)

        val notification: Notification = NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("Foreground Service")
            .setContentText(input)
            .setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.ic_call_to_action)
            .setOngoing(true)
            .build()

        startForeground(1, notification)
        createNotificationChannel()

        return START_NOT_STICKY
    }

    override fun onBind(p0: 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)
        }

    }

}

【问题讨论】:

  • 我认为您对Activity 生命周期有一些误解。只要Activity 不在屏幕上,操作系统就可以随意销毁它。事实上,你应该预料到它会在onStop() 之后不久被销毁。不要试图“解决”这个生命周期;使用它。我认为也许对您来说更重要的是您的 app 没有被破坏,因为您拥有前台服务。尝试将您的 Activity.onCreate() 逻辑(您不希望执行两次的内容)移动到应用程序/服务 onCreate()

标签: android android-activity foreground-service


【解决方案1】:
  1. 尝试在活动清单中设置 android:launchMode="singleInstance"。
  2. 不要忘记为您的活动意图设置一些操作: activityIntent.setAction(ACTION_STARTED_FROM_NOTIFICATION);
  3. 覆盖 Activity 中的 onNewIntent。
  4. 在 onCreate 和 onNewIntent 中检查 if(ACTION_STARTED_FROM_NOTIFICATION.equalsIgnoreCase(intent.getAction())) {做你需要的}

【讨论】:

    猜你喜欢
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 2020-10-28
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多