Jobscheduler simple android app example

Jobscheduler simple android app example with free download option. This Android Development Tutorial covers a new API for scheduling work – a job scheduler – which is also a new feature to better manage battery life.

 

activity_main.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.apple.jobscheduling.MainActivity"
    android:orientation="vertical"
    android:background="@android:color/holo_red_dark">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/b1"
        android:text="StartJob"
        android:onClick="startjob"
        android:background="@android:color/holo_purple"/>

    <Button
        android:id="@+id/b2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="stopjob"
        android:text="StopJob"
        android:layout_marginTop="10dp"
        android:background="@android:color/holo_orange_dark"/>



</LinearLayout>

 

MainActivity.java

package com.example.apple.jobscheduling;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
{
    JobInfo ji;
    JobScheduler js;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    public void startjob(View view)
    {
        js=(JobScheduler)getSystemService(JOB_SCHEDULER_SERVICE);

        ComponentName cn=new ComponentName(this,MyJobService.class);

        JobInfo.Builder jib=new JobInfo.Builder(0,cn);

        jib.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
        jib.setRequiresCharging(true);
        jib.setRequiresDeviceIdle(false);

        ji=jib.build();

        js.schedule(ji);

    }

    public void stopjob(View view)
    {
        //nm.cancel(0);

        if(js != null)
        {
            js.cancelAll();
            js=null;

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



    }
}

MyJobService.java

package com.example.apple.jobscheduling;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Intent;
import android.widget.Toast;

public class MyJobService extends JobService
{
    @Override
    public boolean onStartJob(JobParameters jobParameters)
    {

        Toast.makeText(this,"Job Started",Toast.LENGTH_SHORT).show();
        NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        Intent i=new Intent(this,Main2Activity.class);

        PendingIntent pi=PendingIntent.getActivity(this,0,i,0);

        Notification.Builder nib=new Notification.Builder(this);

        nib.setContentTitle("My Notification");
        nib.setContentText("Phone is charging");
        nib.setSmallIcon(R.mipmap.ic_launcher);

        nib.setContentIntent(pi);

        nib.setAutoCancel(true);

        nm.notify(0,nib.build());

        return false;
    }

    @Override
    public boolean onStopJob(JobParameters jobParameters)
    {

        return false;
    }
}

 

 

This Post Has One Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close Menu