मैं अग्रभूमि सेवाओं के साथ मिलकर एंड्रॉइड के प्रक्रिया प्रबंधन में एक व्यवहार के साथ आया, जो वास्तव में मुझे भ्रमित करता है।
मेरे लिए क्या उचित है?
- जब आप अपने ऐप को 'हाल के ऐप्स' से स्वाइप करते हैं, तो OS को अपेक्षाकृत निकट भविष्य में ऐप प्रक्रिया समाप्त कर देनी चाहिए।
- जब आप अग्रभूमि सेवा चलाते समय अपने ऐप को 'हाल के ऐप्स' से स्वाइप करते हैं, तो ऐप जीवित रहता है।
- जब आप अपने ऐप को 'हाल के ऐप्स' से स्वाइप करने से पहले अग्रभूमि सेवा बंद कर देते हैं तो आपको 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)
}
}
इस प्रसारण को रोकने के लिए प्रसारणकर्ता:
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
(सेवा) कहा जाता है, फिर भी सब कुछ खत्म हो जाने के बाद भी यह प्रक्रिया जीवित रहती है। यहां तक कि अगर सेवा के फिर से शुरू होने की स्थिति में, ओएस प्रक्रिया को जीवित रखे हुए था, तो मैं यह नहीं देखता कि जब आप पहले सेवा को रोकते हैं तो यह वही काम क्यों नहीं करता है, तो एप्लिकेशन को पुनरावृत्ति से हटा दें। प्रदर्शित व्यवहार अप्रभावी लगता है, विशेष रूप से पृष्ठभूमि निष्पादन की सीमाओं में हालिया परिवर्तन, इसलिए यह जानना अच्छा होगा कि यह सुनिश्चित करने के लिए कि प्रक्रिया बंद हो जाती है