-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
KeepAliveService.java
103 lines (89 loc) · 3.68 KB
/
KeepAliveService.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.parishod.watomatic.service;
import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import com.parishod.watomatic.NotificationService;
import com.parishod.watomatic.model.preferences.PreferencesManager;
import com.parishod.watomatic.model.utils.NotificationHelper;
import com.parishod.watomatic.receivers.NotificationServiceRestartReceiver;
public class KeepAliveService extends Service {
private static final int FOREGROUND_NOTIFICATION_ID = 10;
@Override
public void onCreate() {
Log.d("DEBUG", "KeepAliveService onCreate");
super.onCreate();
startForeground(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// super.onStartCommand(intent, flags, startId);
Log.d("DEBUG", "KeepAliveService onStartCommand");
startNotificationService();
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d("DEBUG", "KeepAliveService onBind");
return null;
}
@Override
public boolean onUnbind(Intent intent) {
Log.d("DEBUG", "KeepAliveService onUnbind");
tryReconnectService();
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("DEBUG", "KeepAliveService onDestroy");
tryReconnectService();
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Log.d("DEBUG", "KeepAliveService onTaskRemoved");
tryReconnectService();
}
public void tryReconnectService() {
if (PreferencesManager.getPreferencesInstance(getApplicationContext()).isServiceEnabled()
&& (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S || PreferencesManager.getPreferencesInstance(getApplicationContext()).isForegroundServiceNotificationEnabled())) {
Log.d("DEBUG", "KeepAliveService tryReconnectService");
//Send broadcast to restart service
Intent broadcastIntent = new Intent(getApplicationContext(), NotificationServiceRestartReceiver.class);
broadcastIntent.setAction("Watomatic-RestartService-Broadcast");
sendBroadcast(broadcastIntent);
}
}
private void startNotificationService() {
if (!isMyServiceRunning()) {
Log.d("DEBUG", "KeepAliveService startNotificationService");
Intent mServiceIntent = new Intent(this, NotificationService.class);
startService(mServiceIntent);
}
}
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (NotificationService.class.equals(service.service.getClassName())) {
Log.i("isMyServiceRunning?", true + "");
return true;
}
}
Log.i("isMyServiceRunning?", false + "");
return false;
}
private void startForeground(Service service) {
Log.e("DEBUG", "startForeground");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationCompat.Builder notificationBuilder = NotificationHelper.getInstance(getApplicationContext()).getForegroundServiceNotification(service);
service.startForeground(FOREGROUND_NOTIFICATION_ID, notificationBuilder.build());
}
}
}