Skip to content

Commit bcf239e

Browse files
author
hui.zhao
committed
#Fixed#
NullPointerException in LocalNotificationListenerService: #73
1 parent 363198d commit bcf239e

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

android-godeye/src/main/java/cn/hikyson/godeye/core/internal/notification/LocalNotificationListenerService.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.os.IBinder;
77

88
import androidx.annotation.Nullable;
9+
import androidx.core.content.ContextCompat;
910

1011
import java.util.LinkedList;
1112

@@ -14,6 +15,8 @@
1415
import cn.hikyson.godeye.core.utils.L;
1516

1617
public class LocalNotificationListenerService extends Service {
18+
private static final String ACTION_START = "START_FOREGROUND_ACTION";
19+
private static final String ACTION_STOP = "STOP_FOREGROUND_ACTION";
1720
private int mNotificationId;
1821
private LinkedList<String> mLatestMessages;
1922
private int mCount;
@@ -22,15 +25,13 @@ public static void start(String message, boolean isStartup) {
2225
Intent intent = new Intent(GodEye.instance().getApplication(), LocalNotificationListenerService.class);
2326
intent.putExtra("message", message);
2427
intent.putExtra("isStartup", isStartup);
25-
intent.setAction("START_FOREGROUND_ACTION");
26-
// TODO KYSON DEL
27-
// ContextCompat.startForegroundService(GodEye.instance().getApplication(), intent);
28-
GodEye.instance().getApplication().startService(intent);
28+
intent.setAction(ACTION_START);
29+
ContextCompat.startForegroundService(GodEye.instance().getApplication(), intent);
2930
}
3031

3132
public static void stop() {
3233
Intent intent = new Intent(GodEye.instance().getApplication(), LocalNotificationListenerService.class);
33-
intent.setAction("STOP_FOREGROUND_ACTION");
34+
intent.setAction(ACTION_STOP);
3435
GodEye.instance().getApplication().startService(intent);
3536
}
3637

@@ -44,10 +45,13 @@ public void onCreate() {
4445

4546
@Override
4647
public int onStartCommand(Intent intent, int flags, int startId) {
47-
if ("START_FOREGROUND_ACTION".equals(intent.getAction())) {
48-
// TODO KYSON DEL
49-
// startForeground(mNotificationId, updateNotification(intent));
50-
} else if ("STOP_FOREGROUND_ACTION".equals(intent.getAction())) {
48+
if (intent == null) {
49+
L.w("LocalNotificationListenerService onStartCommand intent == null");
50+
return START_REDELIVER_INTENT;
51+
}
52+
if (ACTION_START.equals(intent.getAction())) {
53+
startForeground(mNotificationId, updateNotification(intent));
54+
} else if (ACTION_STOP.equals(intent.getAction())) {
5155
stopForeground(true);
5256
stopSelf();
5357
}
@@ -80,12 +84,6 @@ private Notification updateNotification(Intent intent) {
8084
return Notifier.create(this, new Notifier.Config(title, sb.toString()));
8185
}
8286

83-
@Override
84-
public void onDestroy() {
85-
super.onDestroy();
86-
L.d("LocalNotificationListenerService onDestroy");
87-
}
88-
8987
@Nullable
9088
@Override
9189
public IBinder onBind(Intent intent) {

0 commit comments

Comments
 (0)