Updated 16 April 2019
Hello All, as we all know that Whenever an app runs in the background, it consumes some of the device’s limited resources, like RAM. To improve the user experience, Android 8.0 (API level 26) imposes limitations on what apps can do while running in the background.
In Oreo devices, you can run the background task using service. So follow the steps:-
You can get more about details using the following code snippets.
Creation of JobScheduler and Job Service
The Android 5.0 Lollipop (API 21) release introduces a job scheduler API via the JobScheduler class. This API allows to batch jobs when the device has more resources available. In general, this API can be used to schedule everything that is not time critical for the user. A unit of work is encapsulated by a JobInfo object. This object specifies the scheduling criteria. The job scheduler allows considering the state of the device. To implement a Job, extend the JobService class and implement the onStartJob and onStopJob methods. If the job fails for some reason, return true from on the onStopJob to restart the job. The onStartJob is performed in the main thread, if you start asynchronous processing in this method, return true otherwise false.The new JobService must be registered in the Android manifest with the BIND_JOB_SERVICE permission.
1 2 3 4 5 6 7 8 9 10 11 |
<service android:name=".MyService" android:stopWithTask="false" android:exported="false" android:permission="android.permission.BIND_JOB_SERVICE"/> <receiver android:name=".MyAlarmReceiver" android:process=":remote"></receiver> <service android:name=".TestJobService" android:exported="true" android:permission="android.permission.BIND_JOB_SERVICE" > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class MyJObScheduler { // schedule the start of the service every 10 - 30 seconds @TargetApi(Build.VERSION_CODES.M) @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public static void scheduleJob(Context context) { ComponentName serviceComponent = new ComponentName(context, MyService.class); JobInfo.Builder builder = new JobInfo.Builder(0, serviceComponent); builder.setMinimumLatency(1 * 1000); // wait at least builder.setOverrideDeadline(3 * 1000); // maximum delay JobScheduler jobScheduler = context.getSystemService(JobScheduler.class); jobScheduler.schedule(builder.build()); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class TestJobService extends JobService { private static final String TAG = "SyncService"; @Override public boolean onStartJob(JobParameters params) { Intent service = new Intent(getApplicationContext(), MyService.class); getApplicationContext().startService(service); // MyJObScheduler.scheduleJob(getApplicationContext()); // reschedule the job return true; } @Override public boolean onStopJob(JobParameters params) { return true; } } |
Create a Broadcast Receiver
Here, we are implementing a broadcast receiver in order to trigger the jobScheduler implementation.
1 2 3 4 5 6 7 8 9 10 11 |
public class MyAlarmReceiver extends BroadcastReceiver { public static final int REQUEST_CODE = 12345; // Triggered by the Alarm periodically (starts the service to run task) @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override public void onReceive(Context context, Intent intent) { Log.d("kkk--", "onReceive: ----- "); MyJObScheduler.scheduleJob(context); } } |
Create A service
From the JobService, we are triggering the execution of a Service background task.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
public class MyService extends Service { private static Timer timer = new Timer(); private Context ctx; int val = 0; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); ctx = this; startService(); } private void startService() { Toast.makeToast(ctx,"Service has been started", Toast.LENGTH_SHORT).show(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; } public void onDestroy() { super.onDestroy(); stopSelf(); Toast.makeText(this, "Service Stopped ...", Toast.LENGTH_SHORT).show(); } } |
You can go throw the following link get more details and find more way to resolve background limitation:-
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
1 comments