6
6
import android .os .IBinder ;
7
7
8
8
import androidx .annotation .Nullable ;
9
+ import androidx .core .content .ContextCompat ;
9
10
10
11
import java .util .LinkedList ;
11
12
14
15
import cn .hikyson .godeye .core .utils .L ;
15
16
16
17
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" ;
17
20
private int mNotificationId ;
18
21
private LinkedList <String > mLatestMessages ;
19
22
private int mCount ;
@@ -22,15 +25,13 @@ public static void start(String message, boolean isStartup) {
22
25
Intent intent = new Intent (GodEye .instance ().getApplication (), LocalNotificationListenerService .class );
23
26
intent .putExtra ("message" , message );
24
27
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 );
29
30
}
30
31
31
32
public static void stop () {
32
33
Intent intent = new Intent (GodEye .instance ().getApplication (), LocalNotificationListenerService .class );
33
- intent .setAction ("STOP_FOREGROUND_ACTION" );
34
+ intent .setAction (ACTION_STOP );
34
35
GodEye .instance ().getApplication ().startService (intent );
35
36
}
36
37
@@ -44,10 +45,13 @@ public void onCreate() {
44
45
45
46
@ Override
46
47
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 ())) {
51
55
stopForeground (true );
52
56
stopSelf ();
53
57
}
@@ -80,12 +84,6 @@ private Notification updateNotification(Intent intent) {
80
84
return Notifier .create (this , new Notifier .Config (title , sb .toString ()));
81
85
}
82
86
83
- @ Override
84
- public void onDestroy () {
85
- super .onDestroy ();
86
- L .d ("LocalNotificationListenerService onDestroy" );
87
- }
88
-
89
87
@ Nullable
90
88
@ Override
91
89
public IBinder onBind (Intent intent ) {
0 commit comments