लंबे समय तक काम करने वाले वर्कर के लिए सहायता

WorkManager में, लंबे समय तक काम करने वाले वर्कर के लिए सहायता पहले से मौजूद होती है. ऐसे मामलों में, WorkManager ओएस को यह सिग्नल दे सकता है कि इस काम को पूरा करने के दौरान, प्रोसेस को चालू रखा जाना चाहिए. ये वर्कर, 10 मिनट से ज़्यादा समय तक चल सकते हैं. इस नई सुविधा के इस्तेमाल के उदाहरणों में, एक साथ कई फ़ाइलें अपलोड या डाउनलोड करना (जिन्हें छोटे-छोटे हिस्सों में नहीं बांटा जा सकता), एमएल मॉडल को स्थानीय तौर पर प्रोसेस करना या ऐप्लिकेशन के उपयोगकर्ता के लिए ज़रूरी कोई टास्क शामिल है.

WorkManager, आपकी ओर से फ़ोरग्राउंड सेवा को मैनेज करता है और उसे चलाता है, ताकि WorkRequest को पूरा किया जा सके. साथ ही, यह कॉन्फ़िगर की जा सकने वाली सूचना भी दिखाता है.

ListenableWorker अब setForegroundAsync() एपीआई के साथ काम करता है. साथ ही, CoroutineWorker, खाते को निलंबित करने वाले setForeground() एपीआई के साथ काम करता है. इन एपीआई की मदद से डेवलपर यह तय कर सकते हैं कि यह WorkRequest ज़रूरी है (उपयोगकर्ता के हिसाब से) या लंबे समय तक चलने वाला है.

2.3.0-alpha03 से, WorkManager आपको PendingIntent बनाने की सुविधा भी देता है. इसका इस्तेमाल, createCancelPendingIntent() एपीआई का इस्तेमाल करके, नया Android कॉम्पोनेंट रजिस्टर किए बिना वर्कर रद्द करने के लिए किया जा सकता है. इस तरीके का इस्तेमाल, खास तौर पर setForegroundAsync() या setForeground() एपीआई के साथ किया जाता है. इनका इस्तेमाल, Worker को रद्द करने के लिए सूचना वाली कार्रवाई जोड़ने के लिए किया जा सकता है.

लंबे समय तक काम करने वाले वर्कर बनाना और उन्हें मैनेज करना

Kotlin या Java में कोडिंग करने के आधार पर, आपको थोड़ा अलग तरीका इस्तेमाल करना होगा.

Kotlin

Kotlin डेवलपर को CoroutineWorker का इस्तेमाल करना चाहिए. setForegroundAsync() का इस्तेमाल करने के बजाय, इस तरीके के निलंबित करने वाले वर्शन 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"
   }
}

Java

ListenableWorker या Worker का इस्तेमाल करने वाले डेवलपर, setForegroundAsync() एपीआई को कॉल कर सकते हैं. यह एपीआई, ListenableFuture<Void> दिखाता है. चालू Notification को अपडेट करने के लिए, setForegroundAsync() को कॉल भी किया जा सकता है.

यहां लंबे समय तक चलने वाले वर्कर का एक सामान्य उदाहरण दिया गया है, जो फ़ाइल डाउनलोड करता है. यह वर्कर, प्रोग्रेस को ट्रैक करता है, ताकि चालू Notification को अपडेट किया जा सके. इससे डाउनलोड की प्रोग्रेस दिखती है.

public class DownloadWorker extends Worker {
   private static final String KEY_INPUT_URL = "KEY_INPUT_URL";
   private static final String KEY_OUTPUT_FILE_NAME = "KEY_OUTPUT_FILE_NAME";

   private NotificationManager notificationManager;

   public DownloadWorker(
       @NonNull Context context,
       @NonNull WorkerParameters parameters) {
           super(context, parameters);
           notificationManager = (NotificationManager)
               context.getSystemService(NOTIFICATION_SERVICE);
   }

   @NonNull
   @Override
   public Result doWork() {
       Data inputData = getInputData();
       String inputUrl = inputData.getString(KEY_INPUT_URL);
       String outputFile = inputData.getString(KEY_OUTPUT_FILE_NAME);
       // Mark the Worker as important
       String progress = "Starting Download";
       setForegroundAsync(createForegroundInfo(progress));
       download(inputUrl, outputFile);
       return Result.success();
   }

   private void download(String inputUrl, String outputFile) {
       // Downloads a file and updates bytes read
       // Calls setForegroundAsync(createForegroundInfo(myProgress))
       // periodically when it needs to update the ongoing Notification.
   }

   @NonNull
   private ForegroundInfo createForegroundInfo(@NonNull String progress) {
       // Build a notification using bytesRead and contentLength

       Context context = getApplicationContext();
       String id = context.getString(R.string.notification_channel_id);
       String title = context.getString(R.string.notification_title);
       String cancel = context.getString(R.string.cancel_download);
       // This PendingIntent can be used to cancel the worker
       PendingIntent intent = WorkManager.getInstance(context)
               .createCancelPendingIntent(getId());

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
           createChannel();
       }

       Notification notification = new NotificationCompat.Builder(context, id)
               .setContentTitle(title)
               .setTicker(title)
               .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 new ForegroundInfo(notificationId, notification);
   }

   @RequiresApi(Build.VERSION_CODES.O)
   private void createChannel() {
       // Create a Notification channel
   }
}

लंबे समय तक चलने वाले वर्कर में फ़ोरग्राउंड सेवा का टाइप जोड़ना

अगर आपका ऐप्लिकेशन, 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

Kotlin

private fun createForegroundInfo(progress: String): ForegroundInfo {
   // ...
   return ForegroundInfo(NOTIFICATION_ID, notification,
           FOREGROUND_SERVICE_TYPE_LOCATION or
FOREGROUND_SERVICE_TYPE_MICROPHONE) }

Java

@NonNull
private ForegroundInfo createForegroundInfo(@NonNull String progress) {
   // Build a notification...
   Notification notification = ...;
   return new ForegroundInfo(NOTIFICATION_ID, notification,
           FOREGROUND_SERVICE_TYPE_LOCATION | FOREGROUND_SERVICE_TYPE_MICROPHONE);
}