PushService.java
3.77 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.glsx.pushsdk.service;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import com.glsx.pushsdk.receiver.GLNetworkConnectChangedReceiver;
import com.blankj.utilcode.util.LogUtils;
import androidx.core.app.NotificationCompat;
@SuppressWarnings("deprecation")
public class PushService extends Service {
public static final String TAG = "PushService";
private GLNetworkConnectChangedReceiver mNetworkConnectChangedReceiver;
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
PushService getService() {
return PushService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
LogUtils.w(TAG, "onStartCommand");
registerRevice();
//GLPushManager.getInstance().init(this);
return super.onStartCommand(intent, flags, startId);
//return START_STICKY;
}
/**
* 注册网络变更广播接收器
*/
private void registerRevice() {
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(
mNetworkConnectChangedReceiver = new GLNetworkConnectChangedReceiver(),
filter);
}
@Override
public void onCreate() {
super.onCreate();
LogUtils.w(TAG, "onCreate");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setForegroundService();
}
}
@androidx.annotation.RequiresApi(api = Build.VERSION_CODES.O)
public void setForegroundService() {
String CHANNEL_ID = "GlPushService";
String CHANNEL_NAME = "running...";
int importance = NotificationManager.IMPORTANCE_LOW;
//构建通知渠道
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance);
channel.setDescription(CHANNEL_ID);
//在创建的通知渠道上发送通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
//builder.setSmallIcon(R.drawable.ic_logo) //设置通知图标
builder.setContentTitle(CHANNEL_ID)//设置通知标题
.setContentText(CHANNEL_NAME)//设置通知内容
.setAutoCancel(true) //用户触摸时,自动关闭
.setOngoing(true);//设置处于运行状态
//向系统注册通知渠道,注册后不能改变重要性以及其他通知行为
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
//将服务置于启动状态 NOTIFICATION_ID指的是创建的通知的ID
startForeground(1, builder.build());
}
@Override
public void onDestroy() {
LogUtils.w(TAG, "onDestroy");
stopForeground(true);
super.onDestroy();
unregisterReceiver(mNetworkConnectChangedReceiver);
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
LogUtils.w(TAG, "onRebind");
}
@Override
public boolean onUnbind(Intent intent) {
LogUtils.w(TAG, "onUnbind");
return super.onUnbind(intent);
}
}