WorkManager में, लंबे समय तक चलने वाले वर्कर के लिए पहले से ही सहायता उपलब्ध है. ऐसे मामलों में, WorkManager ओएस को सिग्नल दे सकता है कि अगर हो सके, तो इस काम को पूरा करते समय प्रोसेस को चालू रखा जाए. ये वर्कर 10 मिनट से ज़्यादा समय तक चल सकते हैं. इस नई सुविधा के इस्तेमाल के उदाहरणों में, एक साथ कई फ़ाइलें अपलोड या डाउनलोड करना (जिन्हें अलग-अलग हिस्सों में नहीं बांटा जा सकता), मशीन लर्निंग मॉडल को स्थानीय तौर पर क्रंच करना या ऐसा टास्क शामिल है जो ऐप्लिकेशन के उपयोगकर्ता के लिए ज़रूरी है.
बैकग्राउंड में, WorkManager आपकी ओर से फ़ोरग्राउंड सेवा को मैनेज और रन करता है, ताकि WorkRequest को लागू किया जा सके. साथ ही, यह कॉन्फ़िगर की जा सकने वाली सूचना भी दिखाता है.
ListenableWorker अब setForegroundAsync() एपीआई के साथ काम करता है. साथ ही, CoroutineWorker, setForeground() एपीआई को निलंबित करने के साथ काम करता है. इन एपीआई की मदद से, डेवलपर यह तय कर सकते हैं कि यह WorkRequest (उपयोगकर्ता के नज़रिए से) ज़रूरी है या लंबे समय तक चलने वाला है.
2.3.0-alpha03 से, WorkManager आपको PendingIntent बनाने की सुविधा भी देता है. इसका इस्तेमाल, createCancelPendingIntent() एपीआई का इस्तेमाल करके, नए Android कॉम्पोनेंट को रजिस्टर किए बिना वर्कर को रद्द करने के लिए किया जा सकता है. यह तरीका खास तौर पर तब काम आता है, जब इसका इस्तेमाल setForeground() एपीआई के साथ किया जाता है. इसका इस्तेमाल, Worker को रद्द करने के लिए सूचना वाली कार्रवाई जोड़ने के लिए किया जा सकता है.
लंबे समय तक चलने वाले वर्कर्स बनाना और उन्हें मैनेज करना
लंबे समय तक चलने वाले वर्कर को कॉन्फ़िगर करने और उसके मौजूदा Notification को अपडेट करने के लिए, CoroutineWorker और निलंबित करने वाले setForeground() तरीके का इस्तेमाल करें.
class DownloadWorker(context: Context, parameters: WorkerParameters) : CoroutineWorker(context, parameters) { private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager override suspend fun doWork(): Result { val inputUrl = inputData.getString(KEY_INPUT_URL) ?: return Result.failure() val outputFile = inputData.getString(KEY_OUTPUT_FILE_NAME) ?: return Result.failure() // Mark the Worker as important. val progress = "Starting Download" setForeground(createForegroundInfo(progress)) download(inputUrl, outputFile) return Result.success() } private fun download(inputUrl: String, outputFile: String) { // Downloads a file and updates bytes read. // Calls setForeground() periodically when it needs to update // the ongoing Notification. } // Creates an instance of ForegroundInfo which can be used to update the // ongoing notification. private fun createForegroundInfo(progress: String): ForegroundInfo { val id = applicationContext.getString(R.string.notification_channel_id) val title = applicationContext.getString(R.string.notification_title) val cancel = applicationContext.getString(R.string.cancel_download) // This PendingIntent can be used to cancel the worker. val intent = WorkManager.getInstance(applicationContext) .createCancelPendingIntent(getId()) // Create a Notification channel if necessary. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { createChannel() } val notification = NotificationCompat.Builder(applicationContext, id) .setContentTitle(title) .setTicker(title) .setContentText(progress) .setSmallIcon(R.drawable.ic_work_notification) .setOngoing(true) // Add the cancel action to the notification which can // be used to cancel the worker. .addAction(android.R.drawable.ic_delete, cancel, intent) .build() return ForegroundInfo(notificationId, notification) } @RequiresApi(Build.VERSION_CODES.O) private fun createChannel() { // Create a Notification channel. } companion object { const val KEY_INPUT_URL = "KEY_INPUT_URL" const val KEY_OUTPUT_FILE_NAME = "KEY_OUTPUT_FILE_NAME" } }
लंबे समय तक चलने वाले वर्कर में फ़ोरग्राउंड सेवा का टाइप जोड़ना
अगर आपका ऐप्लिकेशन Android 14 (एपीआई लेवल 34) या इसके बाद के वर्शन को टारगेट करता है, तो आपको लंबे समय तक चलने वाले सभी वर्कर के लिए, फ़ोरग्राउंड सेवा का टाइप बताना होगा.
अगर आपका ऐप्लिकेशन, Android 10 (एपीआई लेवल 29) या इसके बाद के वर्शन को टारगेट करता है और उसमें लंबे समय तक चलने वाला ऐसा वर्कर शामिल है जिसे जगह की जानकारी ऐक्सेस करने की ज़रूरत है, तो बताएं कि वर्कर location टाइप की फ़ोरग्राउंड सेवा का इस्तेमाल करता है.
अगर आपका ऐप्लिकेशन, Android 11 (एपीआई लेवल 30) या इसके बाद के वर्शन को टारगेट करता है और उसमें लंबे समय तक चलने वाला ऐसा वर्कर शामिल है जिसे कैमरे या माइक्रोफ़ोन के ऐक्सेस की ज़रूरत है, तो फ़ोरग्राउंड सेवा के टाइप के तौर पर, camera या microphone का एलान करें.
फ़ोरग्राउंड सेवा के इन टाइप को जोड़ने के लिए, यहां दिए गए सेक्शन में बताया गया तरीका अपनाएं.
ऐप्लिकेशन मेनिफ़ेस्ट में फ़ोरग्राउंड सेवा के टाइप की जानकारी देना
अपने ऐप्लिकेशन के मेनिफ़ेस्ट में, वर्कर की फ़ोरग्राउंड सेवा का टाइप बताएं. यहां दिए गए उदाहरण में, वर्कर को जगह की जानकारी और माइक्रोफ़ोन का ऐक्सेस चाहिए:
AndroidManifest.xml
<service android:name="androidx.work.impl.foreground.SystemForegroundService" android:foregroundServiceType="location|microphone" tools:node="merge" />
रनटाइम के दौरान फ़ोरग्राउंड सेवा के टाइप तय करना
setForeground() या setForegroundAsync() को कॉल करते समय, पक्का करें कि आपने फ़ोरग्राउंड सेवा का टाइप बताया हो.
MyLocationAndMicrophoneWorker
private fun createForegroundInfo(progress: String): ForegroundInfo { // ... return ForegroundInfo( NOTIFICATION_ID, notification, FOREGROUND_SERVICE_TYPE_LOCATION or FOREGROUND_SERVICE_TYPE_MICROPHONE ) }