Background Job Scheduler Example in android |Android App Development With AndroidCource

Wednesday 25 April 2018

Background Job Scheduler Example in android

How to Create Background Process in Job Scheduler Start Job And Stop Job in android example


What is Job Scheduler in Android


JobScheduler is a Android framework API for scheduling tasks or work. It was first included available in Android 5.0 (API level 21), and remains under active development.JobScheduler shoulb be define in JobService.Your JobService is actually going to be a Service that extends the JobService class. This is what enables the system to perform your work for you, regardless of whether your app is active.


Manifest.xml File To Add Job Service.


<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<service android:name=".MyJobSchedular"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:exported="true"
            android:enabled="true"/>

MainActivity.xml


 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.dipl14.jobscheduledemo.MainActivity">

    <Button
        android:id="@+id/btn_startjob"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="Start Job"
        android:textAllCaps="false"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"/>

    <Button
        android:id="@+id/btn_stopJob"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="Stop Job"
        android:textAllCaps="false"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"/>
</LinearLayout>

Create Object of JobSchedule And Create Component Name And JobInfo Object.Job info is a Container of data passed to the JobScheduler parameters required to schedule work against the calling application. These are constructed using the JobInfo.Builder . You must specify at least one sort of constraint on the JobInfo object that you are creating.


MainActivity.java


package com.example.dipl14.jobscheduledemo;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    public static final int JOB_ID = 101;

    public JobScheduler jobScheduler;
    public JobInfo jobInfo;


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //setAlarm();
        ComponentName componentName = new ComponentName(MainActivity.this,MyJobSchedular.class);
        JobInfo.Builder builder = new JobInfo.Builder(JOB_ID,componentName);

        builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
        builder.setPersisted(true);
        builder.setPeriodic(5000);
        jobInfo = builder.build();
        jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);

        findViewById(R.id.btn_startjob).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              //  setAlarm();

                jobScheduler.schedule(jobInfo);

                
                Toast.makeText(MainActivity.this, "Start Job", Toast.LENGTH_SHORT).show();

            }
        });
        
        findViewById(R.id.btn_stopJob).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                jobScheduler.cancelAll();

                Toast.makeText(MainActivity.this, "Stop Job", Toast.LENGTH_SHORT).show();
            }
        });
    }

 
}

Create MyJobService file to extends JobService and Implements method to OnStartJob And OnStopJob.OnStartJob to continue to running in background Process to Start Job And Cancel Or Stop Job to Cancelled Job with background process.


MyJobService.java



package com.example.dipl14.jobscheduledemo;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    public static final int JOB_ID = 101;

    public JobScheduler jobScheduler;
    public JobInfo jobInfo;


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //setAlarm();
        ComponentName componentName = new ComponentName(MainActivity.this,MyJobSchedular.class);
        JobInfo.Builder builder = new JobInfo.Builder(JOB_ID,componentName);

        builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
        builder.setPersisted(true);
        builder.setPeriodic(5000);
        jobInfo = builder.build();
        jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);

        findViewById(R.id.btn_startjob).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              //  setAlarm();

                jobScheduler.schedule(jobInfo);

                
                Toast.makeText(MainActivity.this, "Start Job", Toast.LENGTH_SHORT).show();

            }
        });
        
        findViewById(R.id.btn_stopJob).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                jobScheduler.cancelAll();

                Toast.makeText(MainActivity.this, "Stop Job", Toast.LENGTH_SHORT).show();
            }
        });
    }

 
}

Output



0 comments:

Post a Comment