If you need to perform a data transfer that may take a long time, you can create a JobScheduler job and identify it as a user-initiated data transfer (UIDT) job. UIDT jobs are intended for longer-duration data transfers that are initiated by the device user, such as downloading a file from a remote server. UIDT jobs were introduced with Android 14 (API level 34).
User-initiated data transfer jobs are started by the user. These jobs require a notification, start immediately, and may be able to run for an extended period of time as system conditions allow. You can run several user-initiated data transfer jobs concurrently.
User initiated jobs must be scheduled while the application is visible to the user (or in one of the allowed conditions). After all constraints are met, user initiated jobs can be executed by the OS, subject to system health restrictions. The system may also use the provided estimated payload size to determine how long the job executes.
उपयोगकर्ता की ओर से किए जाने वाले डेटा ट्रांसफ़र के टास्क शेड्यूल करना
To run a user initiated data-transfer job, do the following:
Make sure your app has declared the
JobService
and associated permissions in its manifest:<service android:name="com.example.app.CustomTransferService" android:permission="android.permission.BIND_JOB_SERVICE" android:exported="false"> ... </service>
Also, define a concrete subclass of
JobService
for your data transfer:Kotlin
class CustomTransferService : JobService() { ... }
Java
class CustomTransferService extends JobService() { .... }
Declare the
RUN_USER_INITIATED_JOBS
permission in the manifest:<manifest ...> <uses-permission android:name="android.permission.RUN_USER_INITIATED_JOBS" /> <application ...> ... </application> </manifest>
Call the
setUserInitiated()
method when building aJobInfo
object. (This method is available beginning with Android 14.) We also recommend that you offer a payload size estimate by callingsetEstimatedNetworkBytes()
while creating your job.Kotlin
val networkRequestBuilder = NetworkRequest.Builder() // Add or remove capabilities based on your requirements. // For example, this code specifies that the job won't run // unless there's a connection to the internet (not just a local // network), and the connection doesn't charge per-byte. .addCapability(NET_CAPABILITY_INTERNET) .addCapability(NET_CAPABILITY_NOT_METERED) .build() val jobInfo = JobInfo.Builder(jobId, ComponentName(mContext, CustomTransferService::class.java)) // ... .setUserInitiated(true) .setRequiredNetwork(networkRequestBuilder) // Provide your estimate of the network traffic here .setEstimatedNetworkBytes(1024 * 1024 * 1024) // ... .build()
Java
NetworkRequest networkRequest = new NetworkRequest.Builder() // Add or remove capabilities based on your requirements. // For example, this code specifies that the job won't run // unless there's a connection to the internet (not just a local // network), and the connection doesn't charge per-byte. .addCapability(NET_CAPABILITY_INTERNET) .addCapability(NET_CAPABILITY_NOT_METERED) .build(); JobInfo jobInfo = JobInfo.Builder(jobId, new ComponentName(mContext, CustomTransferService.class)) // ... .setUserInitiated(true) .setRequiredNetwork(networkRequest) // Provide your estimate of the network traffic here .setEstimatedNetworkBytes(1024 * 1024 * 1024) // ... .build();
While the job is being executed, call
setNotification()
on theJobService
object. CallingsetNotification()
makes the user aware that the job is running, both in the Task Manager and in the status bar notification area.When execution is complete, call
jobFinished()
to signal to the system that the job is complete, or that the job should be rescheduled.Kotlin
class CustomTransferService: JobService() { private val scope = CoroutineScope(Dispatchers.IO) @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) override fun onStartJob(params: JobParameters): Boolean { val notification = Notification.Builder(applicationContext, NOTIFICATION_CHANNEL_ID) .setContentTitle("My user-initiated data transfer job") .setSmallIcon(android.R.mipmap.myicon) .setContentText("Job is running") .build() setNotification(params, notification.id, notification, JobService.JOB_END_NOTIFICATION_POLICY_DETACH) // Execute the work associated with this job asynchronously. scope.launch { doDownload(params) } return true } private suspend fun doDownload(params: JobParameters) { // Run the relevant async download task, then call // jobFinished once the task is completed. jobFinished(params, false) } // Called when the system stops the job. override fun onStopJob(params: JobParameters?): Boolean { // Asynchronously record job-related data, such as the // stop reason. return true // or return false if job should end entirely } }
Java
class CustomTransferService extends JobService{ @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) @Override public boolean onStartJob(JobParameters params) { Notification notification = Notification.Builder(getBaseContext(), NOTIFICATION_CHANNEL_ID) .setContentTitle("My user-initiated data transfer job") .setSmallIcon(android.R.mipmap.myicon) .setContentText("Job is running") .build(); setNotification(params, notification.id, notification, JobService.JOB_END_NOTIFICATION_POLICY_DETACH) // Execute the work associated with this job asynchronously. new Thread(() -> doDownload(params)).start(); return true; } private void doDownload(JobParameters params) { // Run the relevant async download task, then call // jobFinished once the task is completed. jobFinished(params, false); } // Called when the system stops the job. @Override public boolean onStopJob(JobParameters params) { // Asynchronously record job-related data, such as the // stop reason. return true; // or return false if job should end entirely } }
Periodically update the notification to keep the user informed of the job's status and progress. If you cannot determine the transfer size ahead of scheduling the job, or need to update the estimated transfer size, use the new API,
updateEstimatedNetworkBytes()
to update the transfer size after it becomes known.
Recommendations
To run UIDT jobs effectively, do the following:
Clearly define network constraints and job execution constraints to specify when the job should be executed.
Execute the task asynchronously in
onStartJob()
; for example, you can do this by using a coroutine. If you don't run the task asynchronously, the work runs on the main thread and might block it, which can cause an ANR.To avoid running the job longer than necessary, call
jobFinished()
when the transfer finishes, whether it succeeds or fails. That way, the job doesn't run longer than necessary. To discover why a job was stopped, implement theonStopJob()
callback method and callJobParameters.getStopReason()
.
पिछले वर्शन के गेम खेलने की सुविधा
There is currently no Jetpack library that supports UIDT jobs. For this reason, we recommend that you gate your change with code that verifies that you're running on Android 14 or higher. On lower Android versions, you can use WorkManager's foreground service implementation as a fallback approach.
Here's an example of code that checks for the appropriate system version:
Kotlin
fun beginTask() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { scheduleDownloadFGSWorker(context) } else { scheduleDownloadUIDTJob(context) } } private fun scheduleDownloadUIDTJob(context: Context) { // build jobInfo val jobScheduler: JobScheduler = context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler jobScheduler.schedule(jobInfo) } private fun scheduleDownloadFGSWorker(context: Context) { val myWorkRequest = OneTimeWorkRequest.from(DownloadWorker::class.java) WorkManager.getInstance(context).enqueue(myWorkRequest) }
Java
public void beginTask() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { scheduleDownloadFGSWorker(context); } else { scheduleDownloadUIDTJob(context); } } private void scheduleDownloadUIDTJob(Context context) { // build jobInfo JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); jobScheduler.schedule(jobInfo); } private void scheduleDownloadFGSWorker(Context context) { OneTimeWorkRequest myWorkRequest = OneTimeWorkRequest.from(DownloadWorker.class); WorkManager.getInstance(context).enqueue(myWorkRequest) }
UIDT जॉब बंद करना
Both the user and the system can stop user-initiated transfer jobs.
उपयोगकर्ता ने टास्क मैनेजर से
उपयोगकर्ता, टास्क मैनेजर में दिखने वाले, उपयोगकर्ता के शुरू किए गए डेटा ट्रांसफ़र जॉब को रोक सकता है.
जब उपयोगकर्ता रोकें बटन दबाता है, तो सिस्टम ये काम करता है:
- आपके ऐप्लिकेशन की प्रोसेस को तुरंत बंद कर देता है. इसमें, चल रही सभी अन्य जॉब या फ़ोरग्राउंड सेवाएं भी शामिल हैं.
- किसी भी चल रही जॉब के लिए
onStopJob()
को कॉल नहीं करता. - उपयोगकर्ता को दिखने वाले जॉब को फिर से शेड्यूल होने से रोकता है.
इन वजहों से, हमारा सुझाव है कि जॉब के लिए पोस्ट की गई सूचना में कंट्रोल दें, ताकि जॉब को आसानी से रोका और फिर से शेड्यूल किया जा सके.
ध्यान दें कि कुछ खास मामलों में, टास्क मैनेजर में टास्क के बगल में बंद करें बटन नहीं दिखता. इसके अलावा, ऐसा भी हो सकता है कि टास्क मैनेजर में टास्क न दिखे.
सिस्टम के हिसाब से
सामान्य टास्क के उलट, उपयोगकर्ता की ओर से शुरू किए गए डेटा ट्रांसफ़र के टास्क पर ऐप्लिकेशन के स्टैंडबाय बकेट के कोटे का कोई असर नहीं पड़ता. हालांकि, अगर इनमें से कोई भी शर्त पूरी होती है, तो सिस्टम अब भी काम रोक देता है:
- डेवलपर की तय की गई कोई शर्त पूरी नहीं हो रही है.
- सिस्टम यह तय करता है कि डेटा ट्रांसफ़र करने के टास्क को पूरा करने के लिए, नौकरी को ज़रूरत से ज़्यादा समय तक चलाया गया है.
- सिस्टम को सिस्टम की सेहत को प्राथमिकता देनी चाहिए और ज़्यादा तापमान की वजह से काम बंद कर देना चाहिए.
- डिवाइस की मेमोरी कम होने की वजह से, ऐप्लिकेशन की प्रोसेस बंद हो गई है.
जब डिवाइस की मेमोरी कम होने के अलावा किसी अन्य वजह से सिस्टम जॉब को रोक देता है, तो सिस्टम onStopJob()
को कॉल करता है. इसके बाद, सिस्टम उस समय जॉब को फिर से शुरू करने की कोशिश करता है जब सिस्टम को लगता है कि यह सबसे सही समय है. पक्का करें कि onStopJob()
को कॉल न किए जाने पर भी, आपका ऐप्लिकेशन डेटा ट्रांसफ़र की स्थिति को बनाए रख सकता हो. साथ ही, onStartJob()
को फिर से कॉल किए जाने पर, आपका ऐप्लिकेशन इस स्थिति को वापस ला सकता हो.
उपयोगकर्ता की ओर से किए जाने वाले डेटा ट्रांसफ़र के टास्क को शेड्यूल करने की अनुमति देने वाली शर्तें
ऐप्लिकेशन, उपयोगकर्ता से शुरू किए गए डेटा ट्रांसफ़र को सिर्फ़ तब शुरू कर सकते हैं, जब ऐप्लिकेशन दिखने वाली विंडो में हो या कुछ शर्तें पूरी हों:
- अगर कोई ऐप्लिकेशन बैकग्राउंड से गतिविधियां शुरू कर सकता है, तो वह बैकग्राउंड से उपयोगकर्ता के शुरू किए गए डेटा ट्रांसफ़र जॉब भी शुरू कर सकता है.
- अगर किसी ऐप्लिकेशन की गतिविधि, हाल ही में इस्तेमाल किए गए ऐप्लिकेशन की स्क्रीन पर मौजूद किसी मौजूदा टास्क के बैक स्टैक में है, तो इसका मतलब यह नहीं है कि उपयोगकर्ता की ओर से शुरू की गई डेटा ट्रांसफ़र की प्रोसेस शुरू की जा सकती है.
अगर जॉब को ऐसे समय पर चलाने के लिए शेड्यूल किया गया है जब ज़रूरी शर्तें पूरी नहीं होती हैं, तो जॉब पूरा नहीं होता और RESULT_FAILURE
गड़बड़ी कोड दिखाता है.
User-Initiated Data Transfer Jobs के लिए अनुमति वाली पाबंदियां
Android, सबसे सही समय पर चलने वाली नौकरियों के लिए, हर नौकरी के टाइप को कंस्ट्रेंट असाइन करने की सुविधा देता है. ये पाबंदियां, Android 13 से उपलब्ध हैं.
ध्यान दें: यहां दी गई टेबल में, सिर्फ़ उन पाबंदियों की तुलना की गई है जो हर तरह की नौकरी के हिसाब से अलग-अलग होती हैं. सभी कंस्ट्रेंट के बारे में जानने के लिए, JobScheduler डेवलपर पेज या काम से जुड़े कंस्ट्रेंट देखें.
यहां दी गई टेबल में, अलग-अलग तरह की नौकरियों के बारे में बताया गया है. ये नौकरियां, नौकरी से जुड़ी किसी शर्त को पूरा करती हैं. साथ ही, इसमें नौकरी से जुड़ी उन शर्तों के बारे में भी बताया गया है जिन्हें WorkManager पूरा करता है. टेबल को जॉब कंस्ट्रेंट के तरीके के नाम से फ़िल्टर करने के लिए, टेबल से पहले मौजूद खोज बार का इस्तेमाल करें.
उपयोगकर्ता की ओर से किए जाने वाले डेटा ट्रांसफ़र के टास्क के लिए, इन शर्तों को पूरा करना ज़रूरी है:
setBackoffCriteria(JobInfo.BACKOFF_POLICY_EXPONENTIAL)
setClipData()
setEstimatedNetworkBytes()
setMinimumNetworkChunkBytes()
setPersisted()
setNamespace()
setRequiredNetwork()
setRequiredNetworkType()
setRequiresBatteryNotLow()
setRequiresCharging()
setRequiresStorageNotLow()
टेस्ट करना
The following list shows some steps on how to test your app's jobs manually:
- To get the job ID, get the value that is defined upon the job being built.
To run a job immediately, or to retry a stopped job, run the following command in a terminal window:
adb shell cmd jobscheduler run -f APP_PACKAGE_NAME JOB_ID
To simulate the system force-stopping a job (due to system health or out-of-quota conditions), run the following command in a terminal window:
adb shell cmd jobscheduler timeout TEST_APP_PACKAGE TEST_JOB_ID
यह भी देखें:
अन्य संसाधन
उपयोगकर्ता के अनुरोध पर होने वाले डेटा ट्रांसफ़र के बारे में ज़्यादा जानने के लिए, यहां दिए गए अतिरिक्त संसाधन देखें:
- यूआईडीटी इंटिग्रेशन पर केस स्टडी: Google Maps ने उपयोगकर्ता की ओर से शुरू किए गए डेटा ट्रांसफ़र एपीआई का इस्तेमाल करके, डाउनलोड की विश्वसनीयता को 10% तक बेहतर बनाया