Skip to content

Commit 5fe303e

Browse files
author
Paul Ruiz
committed
Added Job Scheduler
1 parent 27ff253 commit 5fe303e

File tree

28 files changed

+525
-7
lines changed

28 files changed

+525
-7
lines changed

AndroidWearDigitalWatchFace/README.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

JobScheduler/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### Tuts+ article: Using the Android JobScheduler
2+
3+
### Instructor: Paul Trebilcox-Ruiz
4+
5+
The JobScheduler API was introduced in Android Lollipop, and is used for batching and scheduling future tasks that run in the background of the device when certain conditions are met.
6+
7+
Available March 2015 on TutsPlus.com

JobScheduler/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

JobScheduler/app/build.gradle

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 21
5+
buildToolsVersion "21.1.2"
6+
7+
defaultConfig {
8+
applicationId "com.tutsplus.jobscheduler"
9+
minSdkVersion 21
10+
targetSdkVersion 21
11+
versionCode 1
12+
versionName "1.0"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
}

JobScheduler/app/proguard-rules.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/paulruiz/Library/Android/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.tutsplus.jobscheduler;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.tutsplus.jobscheduler" >
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@drawable/ic_launcher"
8+
android:label="@string/app_name"
9+
android:theme="@style/AppTheme" >
10+
<activity
11+
android:name=".MainActivity"
12+
android:label="@string/app_name" >
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
20+
<service android:name=".JobSchedulerService"
21+
android:permission="android.permission.BIND_JOB_SERVICE" />
22+
</application>
23+
24+
</manifest>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.tutsplus.jobscheduler;
2+
3+
import android.app.job.JobParameters;
4+
import android.app.job.JobService;
5+
import android.os.Handler;
6+
import android.os.Message;
7+
import android.widget.Toast;
8+
9+
/**
10+
* Created by paulruiz on 3/7/15.
11+
*/
12+
public class JobSchedulerService extends JobService {
13+
14+
private Handler mJobHandler = new Handler( new Handler.Callback() {
15+
@Override
16+
public boolean handleMessage( Message msg ) {
17+
Toast.makeText( getApplicationContext(), "JobService task running", Toast.LENGTH_SHORT ).show();
18+
jobFinished( (JobParameters) msg.obj, false );
19+
return true;
20+
}
21+
} );
22+
23+
@Override
24+
public boolean onStartJob(JobParameters params ) {
25+
mJobHandler.sendMessage( Message.obtain( mJobHandler, 1, params ) );
26+
return true;
27+
}
28+
29+
@Override
30+
public boolean onStopJob( JobParameters params ) {
31+
mJobHandler.removeMessages( 1 );
32+
return false;
33+
}
34+
35+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.tutsplus.jobscheduler;
2+
3+
import android.app.Activity;
4+
import android.app.job.JobInfo;
5+
import android.app.job.JobScheduler;
6+
import android.content.ComponentName;
7+
import android.content.Context;
8+
import android.os.Bundle;
9+
import android.view.Menu;
10+
import android.view.MenuItem;
11+
import android.view.View;
12+
import android.widget.Button;
13+
14+
15+
public class MainActivity extends Activity {
16+
17+
private JobScheduler mJobScheduler;
18+
private Button mScheduleJobButton;
19+
private Button mCancelAllJobsButton;
20+
21+
@Override
22+
protected void onCreate( Bundle savedInstanceState ) {
23+
super.onCreate( savedInstanceState );
24+
setContentView( R.layout.activity_main );
25+
mJobScheduler = (JobScheduler) getSystemService( Context.JOB_SCHEDULER_SERVICE );
26+
mScheduleJobButton = (Button) findViewById( R.id.schedule_job );
27+
mCancelAllJobsButton = (Button) findViewById( R.id.cancel_all );
28+
29+
mScheduleJobButton.setOnClickListener( new View.OnClickListener() {
30+
@Override
31+
public void onClick(View v) {
32+
JobInfo.Builder builder = new JobInfo.Builder( 1,
33+
new ComponentName( getPackageName(), JobSchedulerService.class.getName() ) );
34+
35+
builder.setPeriodic( 3000 );
36+
37+
38+
if( mJobScheduler.schedule( builder.build() ) <= 0 ) {
39+
//If something goes wrong
40+
}
41+
}
42+
});
43+
44+
mCancelAllJobsButton.setOnClickListener( new View.OnClickListener() {
45+
@Override
46+
public void onClick( View v ) {
47+
mJobScheduler.cancelAll();
48+
}
49+
});
50+
}
51+
}
Loading
Loading
Loading
Loading
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:layout_width="match_parent"
3+
android:layout_height="match_parent"
4+
android:orientation="vertical">
5+
6+
<Button
7+
android:id="@+id/schedule_job"
8+
android:layout_width="wrap_content"
9+
android:layout_height="wrap_content"
10+
android:text="Schedule Job"/>
11+
12+
<Button
13+
android:id="@+id/cancel_all"
14+
android:layout_width="wrap_content"
15+
android:layout_height="wrap_content"
16+
android:text="Cancel All"/>
17+
18+
</LinearLayout>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
3+
<item android:id="@+id/action_settings" android:title="@string/action_settings"
4+
android:orderInCategory="100" android:showAsAction="never" />
5+
</menu>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<style name="AppTheme" parent="android:Theme.Material.Light">
4+
</style>
5+
</resources>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<resources>
2+
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
3+
(such as screen margins) for screens with more than 820dp of available width. This
4+
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
5+
<dimen name="activity_horizontal_margin">64dp</dimen>
6+
</resources>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<resources>
2+
<!-- Default screen margins, per the Android Design guidelines. -->
3+
<dimen name="activity_horizontal_margin">16dp</dimen>
4+
<dimen name="activity_vertical_margin">16dp</dimen>
5+
</resources>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<string name="app_name">JobScheduler</string>
5+
<string name="hello_world">Hello world!</string>
6+
<string name="action_settings">Settings</string>
7+
8+
</resources>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<resources>
2+
3+
<!-- Base application theme. -->
4+
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
5+
<!-- Customize your theme here. -->
6+
</style>
7+
8+
</resources>

JobScheduler/build.gradle

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2+
3+
buildscript {
4+
repositories {
5+
jcenter()
6+
}
7+
dependencies {
8+
classpath 'com.android.tools.build:gradle:1.0.0'
9+
10+
// NOTE: Do not place your application dependencies here; they belong
11+
// in the individual module build.gradle files
12+
}
13+
}
14+
15+
allprojects {
16+
repositories {
17+
jcenter()
18+
}
19+
}
53.1 KB
Binary file not shown.

JobScheduler/gradle.properties

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Project-wide Gradle settings.
2+
3+
# IDE (e.g. Android Studio) users:
4+
# Gradle settings configured through the IDE *will override*
5+
# any settings specified in this file.
6+
7+
# For more details on how to configure your build environment visit
8+
# http://www.gradle.org/docs/current/userguide/build_environment.html
9+
10+
# Specifies the JVM arguments used for the daemon process.
11+
# The setting is particularly useful for tweaking memory settings.
12+
# Default value: -Xmx10248m -XX:MaxPermSize=256m
13+
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14+
15+
# When configured, Gradle will run in incubating parallel mode.
16+
# This option should only be used with decoupled projects. More details, visit
17+
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18+
# org.gradle.parallel=true
48.7 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Wed Apr 10 15:27:10 PDT 2013
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip

0 commit comments

Comments
 (0)