├── .gitignore ├── .idea ├── caches │ └── build_file_checksums.ser ├── codeStyles │ └── Project.xml ├── dictionaries │ └── gurleensethi.xml ├── gradle.xml ├── misc.xml └── runConfigurations.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── thetehnocafe │ │ └── gurleensethi │ │ └── workmanager │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── thetehnocafe │ │ │ └── gurleensethi │ │ │ └── workmanager │ │ │ ├── MainActivity.java │ │ │ └── MyWorker.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── thetehnocafe │ └── gurleensethi │ └── workmanager │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/libraries 5 | /.idea/modules.xml 6 | /.idea/workspace.xml 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gurleensethi/android-work-manager/3ade055a129740234761f5f38b124ea0bb495f09/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.idea/dictionaries/gurleensethi.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # android-work-manager 2 | Code for tutorial on Android Work Manager | TheTechnoCafe 3 | 4 | Read the tutorial [here](http://thetechnocafe.com/how-to-use-workmanager-in-android/). 5 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | applicationId "com.thetehnocafe.gurleensethi.workmanager" 7 | minSdkVersion 16 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:28.0.0-alpha3' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.2' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | implementation "android.arch.work:work-runtime:1.0.0-alpha04" 29 | } 30 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/thetehnocafe/gurleensethi/workmanager/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.thetehnocafe.gurleensethi.workmanager; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.thetehnocafe.gurleensethi.workmanager", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/thetehnocafe/gurleensethi/workmanager/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.thetehnocafe.gurleensethi.workmanager; 2 | 3 | import android.app.Notification; 4 | import android.app.NotificationChannel; 5 | import android.app.NotificationManager; 6 | import android.arch.lifecycle.Observer; 7 | import android.support.annotation.Nullable; 8 | import android.support.v4.app.NotificationCompat; 9 | import android.support.v4.app.Person; 10 | import android.support.v7.app.AppCompatActivity; 11 | import android.os.Bundle; 12 | import android.util.Log; 13 | import android.view.View; 14 | import android.widget.Button; 15 | import android.widget.TextView; 16 | import android.widget.Toast; 17 | 18 | import java.time.Duration; 19 | import java.util.UUID; 20 | import java.util.concurrent.TimeUnit; 21 | 22 | import androidx.work.Configuration; 23 | import androidx.work.Constraints; 24 | import androidx.work.Data; 25 | import androidx.work.OneTimeWorkRequest; 26 | import androidx.work.PeriodicWorkRequest; 27 | import androidx.work.WorkManager; 28 | import androidx.work.WorkStatus; 29 | 30 | public class MainActivity extends AppCompatActivity { 31 | 32 | private TextView mTextView; 33 | 34 | @Override 35 | protected void onCreate(Bundle savedInstanceState) { 36 | super.onCreate(savedInstanceState); 37 | setContentView(R.layout.activity_main); 38 | 39 | mTextView = findViewById(R.id.textView); 40 | 41 | Data data = new Data.Builder() 42 | .putString(MyWorker.EXTRA_TITLE, "Message from Activity!") 43 | .putString(MyWorker.EXTRA_TEXT, "Hi! I have come from activity.") 44 | .build(); 45 | 46 | Constraints constraints = new Constraints.Builder() 47 | .setRequiresCharging(true) 48 | .build(); 49 | 50 | final OneTimeWorkRequest simpleRequest = new OneTimeWorkRequest.Builder(MyWorker.class) 51 | .setInputData(data) 52 | .setConstraints(constraints) 53 | .addTag("simple_work") 54 | .build(); 55 | 56 | final PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(MyWorker.class, 12, TimeUnit.HOURS) 57 | .addTag("periodic_work") 58 | .build(); 59 | 60 | final UUID workId = simpleRequest.getId(); 61 | 62 | findViewById(R.id.simpleWorkButton).setOnClickListener(new View.OnClickListener() { 63 | @Override 64 | public void onClick(View view) { 65 | WorkManager.getInstance().enqueue(simpleRequest); 66 | } 67 | }); 68 | 69 | findViewById(R.id.cancelWorkButton).setOnClickListener(new View.OnClickListener() { 70 | @Override 71 | public void onClick(View view) { 72 | //WorkManager.getInstance().cancelAllWorkByTag("simple_work"); 73 | WorkManager.getInstance().cancelWorkById(workId); 74 | } 75 | }); 76 | 77 | findViewById(R.id.periodicWorkButton).setOnClickListener(new View.OnClickListener() { 78 | @Override 79 | public void onClick(View view) { 80 | WorkManager.getInstance().enqueue(periodicWorkRequest); 81 | } 82 | }); 83 | 84 | findViewById(R.id.cancelPeriodicWorkButton).setOnClickListener(new View.OnClickListener() { 85 | @Override 86 | public void onClick(View view) { 87 | WorkManager.getInstance().cancelWorkById(periodicWorkRequest.getId()); 88 | } 89 | }); 90 | 91 | WorkManager.getInstance().getStatusById(simpleRequest.getId()) 92 | .observe(this, new Observer() { 93 | @Override 94 | public void onChanged(@Nullable WorkStatus workStatus) { 95 | if (workStatus != null) { 96 | mTextView.append("SimpleWorkRequest: " + workStatus.getState().name() + "\n"); 97 | } 98 | 99 | if (workStatus != null && workStatus.getState().isFinished()) { 100 | String message = workStatus.getOutputData().getString(MyWorker.EXTRA_OUTPUT_MESSAGE, "Default message"); 101 | mTextView.append("SimpleWorkRequest (Data): " + message); 102 | } 103 | } 104 | }); 105 | } 106 | } -------------------------------------------------------------------------------- /app/src/main/java/com/thetehnocafe/gurleensethi/workmanager/MyWorker.java: -------------------------------------------------------------------------------- 1 | package com.thetehnocafe.gurleensethi.workmanager; 2 | 3 | import android.app.Notification; 4 | import android.app.NotificationChannel; 5 | import android.app.NotificationManager; 6 | import android.content.Context; 7 | import android.os.Build; 8 | import android.support.annotation.NonNull; 9 | import android.support.v4.app.NotificationCompat; 10 | import android.util.Log; 11 | 12 | import androidx.work.Data; 13 | import androidx.work.Worker; 14 | 15 | import static android.content.Context.NOTIFICATION_SERVICE; 16 | 17 | public class MyWorker extends Worker { 18 | 19 | public static final String EXTRA_TITLE = "title"; 20 | public static final String EXTRA_TEXT = "text"; 21 | public static final String EXTRA_OUTPUT_MESSAGE = "output_message"; 22 | 23 | @NonNull 24 | @Override 25 | public Result doWork() { 26 | //Executed on different thread 27 | 28 | try { 29 | Thread.sleep(5000); 30 | } catch (InterruptedException e) { 31 | e.printStackTrace(); 32 | } 33 | 34 | String title = getInputData().getString(EXTRA_TITLE, "Default Title"); 35 | String text = getInputData().getString(EXTRA_TEXT, "Default Text"); 36 | 37 | //sendNotification("Simple Work Manager", "I have been send by WorkManager!"); 38 | sendNotification(title, text); 39 | 40 | Data output = new Data.Builder() 41 | .putString(EXTRA_OUTPUT_MESSAGE, "I have come from MyWorker!") 42 | .build(); 43 | 44 | setOutputData(output); 45 | 46 | return Result.SUCCESS; 47 | } 48 | 49 | public void sendNotification(String title, String message) { 50 | NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 51 | 52 | //If on Oreo then notification required a notification channel. 53 | if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { 54 | NotificationChannel channel = new NotificationChannel("default", "Default", NotificationManager.IMPORTANCE_DEFAULT); 55 | notificationManager.createNotificationChannel(channel); 56 | } 57 | 58 | NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(), "default") 59 | .setContentTitle(title) 60 | .setContentText(message) 61 | .setSmallIcon(R.mipmap.ic_launcher); 62 | 63 | notificationManager.notify(1, notification.build()); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 15 | 16 |