-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForegroundService.java
64 lines (53 loc) · 1.74 KB
/
ForegroundService.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
package com.yy.httpproxy.service;
import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import com.yy.httpproxy.util.Log;
public class ForegroundService extends Service {
private final String TAG = "ForegroundService";
public static ConnectionService instance;
@Override
public void onCreate() {
super.onCreate();
try {
if (Build.VERSION.SDK_INT < 26) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setPriority(Notification.PRIORITY_MIN);
startForeground(12345, builder.build());
beginForeground();
}
} catch (Exception e) {
Log.e(TAG, "startForeground error");
}
stopSelf();
Log.i(TAG, "FakeService onCreate");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
return Service.START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "FakeService onDestroy");
stopForeground(true);
}
public void beginForeground() {
if (instance != null) {
if (Build.VERSION.SDK_INT < 26) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(instance);
builder.setPriority(Notification.PRIORITY_MIN);
instance.startForeground(12345, builder.build());
}
}
}
}