我在使用Android的进程管理与前台服务时发现了一个行为,这让我非常困惑。
对我来说是合理的
- 当你从“最近使用的应用”中滑动移除应用时,操作系统应该会在不久的将来结束应用程序的进程。
- 当你在运行前台服务时从“最近使用的应用”中滑动移除应用程序时,应用程序仍然保持活动状态。
- 当你在从“最近使用的应用”中滑动移除应用程序之前停止前台服务时,你将得到与1)相同的结果。
让我感到困惑的是
当你没有正在运行的前台活动(在“最近使用的应用”中不显示应用程序)并停止了前台服务时,我希望应用程序现在已经被杀死了。
但是,这并没有发生,应用程序的进程仍然活着。
示例
我创建了一个最小的示例,展示了这种行为。
前台服务:
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.IBinder
import androidx.core.app.NotificationCompat
import timber.log.Timber
class MyService : Service() {
override fun onBind(intent: Intent?): IBinder? = null
override fun onCreate() {
super.onCreate()
Timber.d("onCreate")
}
override fun onDestroy() {
super.onDestroy()
Timber.d("onDestroy")
// just to make sure the service really stops
stopForeground(true)
stopSelf()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Timber.d("onStartCommand")
startForeground(ID, serviceNotification())
return START_NOT_STICKY
}
private fun serviceNotification(): Notification {
createChannel()
val stopServiceIntent = PendingIntent.getBroadcast(
this,
0,
Intent(this, StopServiceReceiver::class.java),
PendingIntent.FLAG_UPDATE_CURRENT
)
return NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("This is my service")
.setContentText("It runs as a foreground service.")
.addAction(0, "Stop", stopServiceIntent)
.build()
}
private fun createChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = getSystemService(NotificationManager::class.java)
notificationManager.createNotificationChannel(
NotificationChannel(
CHANNEL_ID,
"Test channel",
NotificationManager.IMPORTANCE_DEFAULT
)
)
}
}
companion object {
private const val ID = 532207
private const val CHANNEL_ID = "test_channel"
fun newIntent(context: Context) = Intent(context, MyService::class.java)
}
}
停止服务的BroadcastReceiver:
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
class StopServiceReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val serviceIntent = MyService.newIntent(context)
context.stopService(serviceIntent)
}
}
这个活动:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
startService(MyService.newIntent(this))
}
}
清单:<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.christophlutz.processlifecycletest">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService"/>
<receiver android:name=".StopServiceReceiver" />
</application>
</manifest>
请尝试以下两种方式:
- 启动应用程序,停止前台服务,将应用程序从“最近使用的应用”中删除
- 启动应用程序,将应用程序从“最近使用的应用”中删除,停止前台服务
您可以在Android Studio的LogCat中看到,在情况1下,应用程序进程被标记为[DEAD],而在情况2下没有。
既然很容易复制,这可能是一种有意的行为,但我没有在文档中找到任何真正的提及此事的地方。
有人知道这里发生了什么吗?
onDestroy
(服务)被调用,但进程仍然保持活动状态,即使一切都已经消失。即使操作系统保持进程处于活动状态以便重新启动服务,我也不明白为什么当您先停止服务,然后从最近使用的应用程序中删除应用程序时,它不会执行相同的操作。显示的行为似乎相当不直观,特别是考虑到最近对后台执行限制的更改,因此了解如何确保进程停止将是很好的。 - David MedenjakgetApplicationContext().startService(MyService.newIntent(this));
代替您正在使用的内容,并告诉我结果。我认为活动仍然保持活动状态,因为使用了活动上下文而不是应用程序上下文。另外,如果您正在测试Oreo或更高版本,请尝试使用getApplicationContext().startforegroundService(MyService.newIntent(this));
。 - Furkan Yurdakul