-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyWorker.java
More file actions
50 lines (41 loc) · 1.68 KB
/
MyWorker.java
File metadata and controls
50 lines (41 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.manoj.workmanager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
/**
* This is Worker class where we define our background Task.
* For this class we need dependency that is 'androidx.work:work-runtime:2.2.0'
* or higher( Which is alpha version and not recommended )
*
* */
public class MyWorker extends Worker {
public MyWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
//here we define background task
displayNotification("WorkManager","Background task has been completed");
return Result.success();
}
//Sample notification for testing purpose
private void displayNotification(String task, String desp){
NotificationManager manager = (NotificationManager) getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel("Test","Test",NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "Test")
.setContentTitle(task)
.setContentText(desp)
.setSmallIcon(R.mipmap.ic_launcher);
manager.notify(1, builder.build());
}
}