├── .gitignore ├── .idea ├── caches │ └── build_file_checksums.ser ├── codeStyles │ └── Project.xml ├── gradle.xml ├── misc.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── Screenshots ├── Screenshot_20180809-184251.png ├── Screenshot_20180809-184355.png └── Screenshot_20180809-184428.png ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── krunal │ │ └── com │ │ └── example │ │ └── workmanager │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── krunal │ │ │ └── com │ │ │ └── example │ │ │ └── workmanager │ │ │ ├── MainActivity.java │ │ │ ├── MyPeriodicWork.java │ │ │ ├── MyWork.java │ │ │ ├── MyWorkA.java │ │ │ ├── MyWorkB.java │ │ │ ├── MyWorkC.java │ │ │ └── MyWorkWithData.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 │ └── krunal │ └── com │ └── example │ └── 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/krunalpatel3/WorkManager-Example-Andorid/88f271ca24f0cb11850ce4317ffefe1f1d25d38e/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
7 | 8 | 9 | 10 | xmlns:android 11 | 12 | ^$ 13 | 14 | 15 | 16 |
17 |
18 | 19 | 20 | 21 | xmlns:.* 22 | 23 | ^$ 24 | 25 | 26 | BY_NAME 27 | 28 |
29 |
30 | 31 | 32 | 33 | .*:id 34 | 35 | http://schemas.android.com/apk/res/android 36 | 37 | 38 | 39 |
40 |
41 | 42 | 43 | 44 | .*:name 45 | 46 | http://schemas.android.com/apk/res/android 47 | 48 | 49 | 50 |
51 |
52 | 53 | 54 | 55 | name 56 | 57 | ^$ 58 | 59 | 60 | 61 |
62 |
63 | 64 | 65 | 66 | style 67 | 68 | ^$ 69 | 70 | 71 | 72 |
73 |
74 | 75 | 76 | 77 | .* 78 | 79 | ^$ 80 | 81 | 82 | BY_NAME 83 | 84 |
85 |
86 | 87 | 88 | 89 | .* 90 | 91 | http://schemas.android.com/apk/res/android 92 | 93 | 94 | ANDROID_ATTRIBUTE_ORDER 95 | 96 |
97 |
98 | 99 | 100 | 101 | .* 102 | 103 | .* 104 | 105 | 106 | BY_NAME 107 | 108 |
109 |
110 |
111 |
112 |
113 |
-------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WorkManager Example Andorid. 2 | 3 | How to use WorkManager in Android. This is new way to run background tasks in android and also the best way. 4 | 5 | - It show's you:- 6 | 7 | 1) How to perform OneTimeWorkRequest. 8 | 2) How to perform PeriodicWorkRequest. 9 | 3) How to perform parallel task. 10 | 4) How to perform task one after other. 11 | 5) How to cancel runing task. 12 | 6) How to get work status weather work completed or Not. 13 | 7) How to use Constraints for conditions. 14 | 8) How to send Data from Activity to your Own Worker Class and Also How to send that data back to the Activity from your Own Worker Class. 15 | 9) How send notification using WorkManager. 16 | 17 | ### Note:- 18 | 19 | To See Output you have to see logs in Android Studio. 20 | 21 | ### Reference:- 22 | 23 | 1) https://www.youtube.com/watch?v=fAQKvBHeg_w 24 | 2) https://www.youtube.com/watch?v=1VVir3-4hII 25 | 3) https://www.youtube.com/watch?v=0jgkQYebYvQ 26 | 4) https://www.youtube.com/watch?v=ooP8kkhvRQI 27 | 5) http://thetechnocafe.com/how-to-use-workmanager-in-android/ 28 | 29 | ## Screenshots:- 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Screenshots/Screenshot_20180809-184251.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krunalpatel3/WorkManager-Example-Andorid/88f271ca24f0cb11850ce4317ffefe1f1d25d38e/Screenshots/Screenshot_20180809-184251.png -------------------------------------------------------------------------------- /Screenshots/Screenshot_20180809-184355.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krunalpatel3/WorkManager-Example-Andorid/88f271ca24f0cb11850ce4317ffefe1f1d25d38e/Screenshots/Screenshot_20180809-184355.png -------------------------------------------------------------------------------- /Screenshots/Screenshot_20180809-184428.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krunalpatel3/WorkManager-Example-Andorid/88f271ca24f0cb11850ce4317ffefe1f1d25d38e/Screenshots/Screenshot_20180809-184428.png -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | defaultConfig { 6 | applicationId "krunal.com.example.workmanager" 7 | minSdkVersion 15 8 | targetSdkVersion 27 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 | compileOptions { 20 | sourceCompatibility JavaVersion.VERSION_1_8 21 | targetCompatibility JavaVersion.VERSION_1_8 22 | } 23 | } 24 | 25 | dependencies { 26 | implementation fileTree(include: ['*.jar'], dir: 'libs') 27 | implementation 'com.android.support:appcompat-v7:27.1.1' 28 | implementation 'com.android.support.constraint:constraint-layout:1.1.2' 29 | testImplementation 'junit:junit:4.12' 30 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 31 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 32 | 33 | // For WorkManager. 34 | implementation 'android.arch.work:work-runtime:1.0.0-beta02' 35 | } 36 | -------------------------------------------------------------------------------- /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/krunal/com/example/workmanager/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package krunal.com.example.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("krunal.com.example.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/krunal/com/example/workmanager/MainActivity.java: -------------------------------------------------------------------------------- 1 | package krunal.com.example.workmanager; 2 | 3 | 4 | import android.arch.lifecycle.Observer; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.os.Bundle; 8 | import android.widget.Button; 9 | import android.widget.TextView; 10 | import android.widget.Toast; 11 | 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | import java.util.UUID; 15 | import java.util.concurrent.TimeUnit; 16 | 17 | import androidx.work.Constraints; 18 | import androidx.work.Data; 19 | import androidx.work.NetworkType; 20 | import androidx.work.OneTimeWorkRequest; 21 | import androidx.work.PeriodicWorkRequest; 22 | import androidx.work.WorkInfo; 23 | import androidx.work.WorkManager; 24 | 25 | 26 | public class MainActivity extends AppCompatActivity { 27 | 28 | private Button mOneTimeWork, mPeriodicWork, 29 | mChainableWork, mParallelWork, 30 | mCancelPeriodicWork, mWorkWithConstraints, mWorkWithData; 31 | 32 | private UUID getId; 33 | 34 | private PeriodicWorkRequest mPeriodicWorkRequest; 35 | 36 | private TextView mTextView; 37 | 38 | @Override 39 | protected void onCreate(Bundle savedInstanceState) { 40 | super.onCreate(savedInstanceState); 41 | setContentView(R.layout.activity_main); 42 | 43 | mTextView = findViewById(R.id.textView); 44 | mOneTimeWork = findViewById(R.id.OneTimeWork); 45 | mPeriodicWork = findViewById(R.id.PeriodicWork); 46 | mChainableWork = findViewById(R.id.ChainableWork); 47 | mParallelWork = findViewById(R.id.parallelwork); 48 | mCancelPeriodicWork = findViewById(R.id.CancelPeriodicWork); 49 | mWorkWithConstraints = findViewById(R.id.workwithconstraints); 50 | mWorkWithData = findViewById(R.id.WorkWithData); 51 | 52 | 53 | mOneTimeWork.setOnClickListener(v -> { 54 | // This is One Time Work Request. 55 | OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(MyWork.class) 56 | .build(); 57 | 58 | WorkManager.getInstance().enqueue(oneTimeWorkRequest); 59 | 60 | }); 61 | 62 | mPeriodicWork.setOnClickListener(v -> { 63 | // This is PeriodicWorkRequest it repeats every 10 Hours. 64 | mPeriodicWorkRequest = new PeriodicWorkRequest.Builder(MyPeriodicWork.class, 65 | 10, TimeUnit.HOURS) 66 | .addTag("periodicWorkRequest") 67 | .build(); 68 | 69 | WorkManager.getInstance().enqueue(mPeriodicWorkRequest); 70 | 71 | }); 72 | 73 | mChainableWork.setOnClickListener(v -> { 74 | // This is Chainable Work Request it Runs one after author in sequence. 75 | OneTimeWorkRequest MyWorkA = new OneTimeWorkRequest.Builder(MyWorkA.class) 76 | .build(); 77 | OneTimeWorkRequest MyWorkB = new OneTimeWorkRequest.Builder(MyWorkB.class) 78 | .build(); 79 | OneTimeWorkRequest MyWorkC = new OneTimeWorkRequest.Builder(MyWorkC.class) 80 | .build(); 81 | 82 | WorkManager.getInstance() 83 | .beginWith(MyWorkA) 84 | .then(MyWorkB) 85 | .then(MyWorkC) 86 | .enqueue(); 87 | }); 88 | 89 | mParallelWork.setOnClickListener(v -> { 90 | // Here MyWorkA and MyWorkB will run in Parallel and then MyWorkC will run. 91 | OneTimeWorkRequest MyWorkA = new OneTimeWorkRequest.Builder(MyWorkA.class) 92 | .build(); 93 | OneTimeWorkRequest MyWorkB = new OneTimeWorkRequest.Builder(MyWorkB.class) 94 | .build(); 95 | OneTimeWorkRequest MyWorkC = new OneTimeWorkRequest.Builder(MyWorkC.class) 96 | .build(); 97 | 98 | List beginWith_A_and_B = new ArrayList<>(); 99 | beginWith_A_and_B.add(MyWorkA); 100 | beginWith_A_and_B.add(MyWorkB); 101 | 102 | WorkManager.getInstance() 103 | .beginWith(beginWith_A_and_B) 104 | .then(MyWorkC) 105 | .enqueue(); 106 | 107 | 108 | 109 | }); 110 | 111 | mCancelPeriodicWork.setOnClickListener(v -> { 112 | // We are Canceling PeriodicWork by Id. 113 | getId = mPeriodicWorkRequest.getId(); 114 | WorkManager.getInstance().cancelWorkById(getId); 115 | Toast.makeText(this, "PeriodicWork Cancel By Id", Toast.LENGTH_LONG).show(); 116 | }); 117 | 118 | mWorkWithConstraints.setOnClickListener(v -> { 119 | // We are putting Constraints means than if the device is 120 | // CONNECTED to internet than only we Request to do Work. 121 | // And We are also Checking for the status if the Work Have completed or not. 122 | Constraints constraints = new Constraints.Builder() 123 | .setRequiredNetworkType(NetworkType.CONNECTED) 124 | .build(); 125 | 126 | OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(MyWork.class) 127 | .setConstraints(constraints) 128 | .build(); 129 | 130 | WorkManager.getInstance().enqueue(oneTimeWorkRequest); 131 | 132 | WorkManager.getInstance().getWorkInfoByIdLiveData(oneTimeWorkRequest.getId()) 133 | .observe(this, new Observer() { 134 | @Override 135 | public void onChanged(@Nullable WorkInfo workInfo) { 136 | 137 | if (workInfo != null) { 138 | Toast.makeText(MainActivity.this, "oneTimeWorkRequest: " + 139 | String.valueOf(workInfo.getState().name()), Toast.LENGTH_LONG) 140 | .show(); 141 | } 142 | 143 | if (workInfo != null && workInfo.getState().isFinished()) { 144 | // ... do something with the result ... 145 | Toast.makeText(MainActivity.this, "Work Finished", 146 | Toast.LENGTH_LONG).show(); 147 | } 148 | 149 | } 150 | }); 151 | 152 | 153 | }); 154 | 155 | mWorkWithData.setOnClickListener(v -> { 156 | // Here we are Passing Data to MyWorkWithData class. 157 | // We are also getting Data from MyWorkWithData class. 158 | // After getting Data from MyWorkWithData class we are setting to TextView. 159 | Data data = new Data.Builder() 160 | .putString(MyWorkWithData.EXTRA_TITLE, "I came From Activity!") 161 | .putString(MyWorkWithData.EXTRA_TEXT, "This is Message.") 162 | .build(); 163 | 164 | OneTimeWorkRequest oneTimeWorkRequest = 165 | new OneTimeWorkRequest.Builder(MyWorkWithData.class) 166 | .setInputData(data) 167 | .build(); 168 | 169 | WorkManager.getInstance().enqueue(oneTimeWorkRequest); 170 | 171 | 172 | WorkManager.getInstance().getWorkInfoByIdLiveData(oneTimeWorkRequest.getId()) 173 | .observe(this, new Observer() { 174 | @Override 175 | public void onChanged(@Nullable WorkInfo workInfo) { 176 | 177 | if (workInfo != null && workInfo.getState().isFinished()) { 178 | String message = workInfo.getOutputData().getString(MyWorkWithData.EXTRA_OUTPUT_MESSAGE); 179 | mTextView.setText(message); 180 | } 181 | 182 | } 183 | }); 184 | 185 | }); 186 | 187 | } 188 | } 189 | -------------------------------------------------------------------------------- /app/src/main/java/krunal/com/example/workmanager/MyPeriodicWork.java: -------------------------------------------------------------------------------- 1 | package krunal.com.example.workmanager; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | import android.util.Log; 6 | 7 | import androidx.work.Worker; 8 | import androidx.work.WorkerParameters; 9 | 10 | public class MyPeriodicWork extends Worker { 11 | 12 | private static final String TAB = MyPeriodicWork.class.getSimpleName(); 13 | 14 | public MyPeriodicWork(@NonNull Context context, @NonNull WorkerParameters workerParams) { 15 | super(context, workerParams); 16 | } 17 | 18 | 19 | @NonNull 20 | @Override 21 | public Result doWork() { 22 | 23 | Log.e(TAB,"PeriodicWork in BackGround"); 24 | 25 | return Result.success(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/java/krunal/com/example/workmanager/MyWork.java: -------------------------------------------------------------------------------- 1 | package krunal.com.example.workmanager; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | import android.util.Log; 6 | import androidx.work.Worker; 7 | import androidx.work.WorkerParameters; 8 | 9 | public class MyWork extends Worker { 10 | 11 | private static final String TAB = MyWork.class.getSimpleName(); 12 | 13 | public MyWork(@NonNull Context context, @NonNull WorkerParameters workerParams) { 14 | super(context, workerParams); 15 | } 16 | 17 | @NonNull 18 | @Override 19 | public Result doWork() { 20 | 21 | Log.e(TAB,"Working in BackGround"); 22 | 23 | return Result.success(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/krunal/com/example/workmanager/MyWorkA.java: -------------------------------------------------------------------------------- 1 | package krunal.com.example.workmanager; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | import android.util.Log; 6 | 7 | import androidx.work.Worker; 8 | import androidx.work.WorkerParameters; 9 | 10 | public class MyWorkA extends Worker { 11 | 12 | private static final String TAB = MyWorkA.class.getSimpleName(); 13 | 14 | public MyWorkA(@NonNull Context context, @NonNull WorkerParameters workerParams) { 15 | super(context, workerParams); 16 | } 17 | 18 | @NonNull 19 | @Override 20 | public Result doWork() { 21 | 22 | Log.e(TAB,"My WorkA"); 23 | 24 | return Result.success(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/krunal/com/example/workmanager/MyWorkB.java: -------------------------------------------------------------------------------- 1 | package krunal.com.example.workmanager; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | import android.util.Log; 6 | 7 | import androidx.work.Worker; 8 | import androidx.work.WorkerParameters; 9 | 10 | public class MyWorkB extends Worker { 11 | 12 | private static final String TAB = MyWorkB.class.getSimpleName(); 13 | 14 | public MyWorkB(@NonNull Context context, @NonNull WorkerParameters workerParams) { 15 | super(context, workerParams); 16 | } 17 | 18 | @NonNull 19 | @Override 20 | public Result doWork() { 21 | 22 | Log.e(TAB,"My WorkB"); 23 | 24 | return Result.success(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/krunal/com/example/workmanager/MyWorkC.java: -------------------------------------------------------------------------------- 1 | package krunal.com.example.workmanager; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | import android.util.Log; 6 | 7 | import androidx.work.Worker; 8 | import androidx.work.WorkerParameters; 9 | 10 | public class MyWorkC extends Worker { 11 | 12 | private static final String TAB = MyWorkC.class.getSimpleName(); 13 | 14 | public MyWorkC(@NonNull Context context, @NonNull WorkerParameters workerParams) { 15 | super(context, workerParams); 16 | } 17 | 18 | @NonNull 19 | @Override 20 | public Result doWork() { 21 | 22 | Log.e(TAB,"My WorkC"); 23 | 24 | return Result.success(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/krunal/com/example/workmanager/MyWorkWithData.java: -------------------------------------------------------------------------------- 1 | package krunal.com.example.workmanager; 2 | 3 | import android.app.NotificationChannel; 4 | import android.app.NotificationManager; 5 | import android.content.Context; 6 | import android.support.annotation.NonNull; 7 | import android.support.v4.app.NotificationCompat; 8 | 9 | import androidx.work.Data; 10 | import androidx.work.Worker; 11 | import androidx.work.WorkerParameters; 12 | 13 | public class MyWorkWithData extends Worker { 14 | 15 | private static final String TAB = MyWorkWithData.class.getSimpleName(); 16 | static final String EXTRA_TITLE = "title"; 17 | static final String EXTRA_TEXT = "text"; 18 | static final String EXTRA_OUTPUT_MESSAGE = "output_message"; 19 | 20 | 21 | public MyWorkWithData(@NonNull Context context, @NonNull WorkerParameters workerParams) { 22 | super(context, workerParams); 23 | } 24 | 25 | @NonNull 26 | @Override 27 | public Result doWork() { 28 | 29 | // Getting Data from MainActivity. 30 | String title = getInputData().getString(EXTRA_TITLE); 31 | String text = getInputData().getString(EXTRA_TEXT); 32 | 33 | sendNotification(title, text); 34 | 35 | Data output = new Data.Builder() 36 | .putString(EXTRA_OUTPUT_MESSAGE, "I have come from MyWorkWithData Class!") 37 | .build(); 38 | 39 | // Sending Data to MainActivity. 40 | return Result.success(output); 41 | } 42 | 43 | private void sendNotification(String title, String message) { 44 | NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 45 | 46 | //If on Oreo then notification required a notification channel. 47 | if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { 48 | NotificationChannel channel = new NotificationChannel("default", "Default", NotificationManager.IMPORTANCE_DEFAULT); 49 | notificationManager.createNotificationChannel(channel); 50 | } 51 | 52 | NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(), "default") 53 | .setContentTitle(title) 54 | .setContentText(message) 55 | .setSmallIcon(R.mipmap.ic_launcher); 56 | 57 | notificationManager.notify(1, notification.build()); 58 | } 59 | 60 | 61 | 62 | 63 | } 64 | -------------------------------------------------------------------------------- /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 |