-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestartServiceBroadcastReceiver.java
85 lines (81 loc) · 3.89 KB
/
RestartServiceBroadcastReceiver.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.example.offline;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import androidx.annotation.RequiresApi;
import static android.content.Context.JOB_SCHEDULER_SERVICE;
public class RestartServiceBroadcastReceiver extends BroadcastReceiver {
public static final String TAG = RestartServiceBroadcastReceiver.class.getSimpleName();
private static JobScheduler jobScheduler;
private RestartServiceBroadcastReceiver restartSensorServiceReceiver;
@Override
public void onReceive(final Context context, Intent intent) {
Log.d(TAG, "about to start timer " + context.toString());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
scheduleJob(context);
} else {
registerRestarterReceiver(context);
ProcessMainClass bck = new ProcessMainClass();
bck.launchService(context);
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static void scheduleJob(Context context) {
if (jobScheduler == null) {
jobScheduler = (JobScheduler) context
.getSystemService(JOB_SCHEDULER_SERVICE);
}
ComponentName componentName = new ComponentName(context,
myJobService.class);
JobInfo jobInfo = new JobInfo.Builder(1, componentName)
// setOverrideDeadline runs it immediately - you must have at least one constraint
// https://stackoverflow.com/questions/51064731/firing-jobservice-without-constraints
.setOverrideDeadline(0)
.setPersisted(true).build();
jobScheduler.schedule(jobInfo);
}
public static void reStartTracker(Context context) {
// restart the never ending service
Log.i(TAG, "Restarting tracker");
Intent broadcastIntent = new Intent(MainActivity.MyGlobalVariables.RESTART_INTENT);
context.sendBroadcast(broadcastIntent);
}
private void registerRestarterReceiver(final Context context) {
// the context can be null if app just installed and this is called from restartsensorservice
// https://stackoverflow.com/questions/24934260/intentreceiver-components-are-not-allowed-to-register-to-receive-intents-when
// Final decision: in case it is called from installation of new version (i.e. from manifest, the application is
// null. So we must use context.registerReceiver. Otherwise this will crash and we try with context.getApplicationContext
if (restartSensorServiceReceiver == null)
restartSensorServiceReceiver = new RestartServiceBroadcastReceiver();
else try{
context.unregisterReceiver(restartSensorServiceReceiver);
} catch (Exception e){
// not registered
}
// give the time to run
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// we register the receiver that will restart the background service if it is killed
// see onDestroy of Service
IntentFilter filter = new IntentFilter();
filter.addAction(MainActivity.MyGlobalVariables.RESTART_INTENT);
try {
context.registerReceiver(restartSensorServiceReceiver, filter);
} catch (Exception e) {
try {
context.getApplicationContext().registerReceiver(restartSensorServiceReceiver, filter);
} catch (Exception ex) {
}
}
}
}, 1000);
}
}