|
@@ -26,32 +26,32 @@ public class ForegroundService extends Service {
|
|
//服务通信
|
|
//服务通信
|
|
@Nullable
|
|
@Nullable
|
|
@Override
|
|
@Override
|
|
- public IBinder onBind(Intent intent){
|
|
|
|
|
|
+ public IBinder onBind(Intent intent) {
|
|
//与Activity进行通信
|
|
//与Activity进行通信
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
//服务创建时
|
|
//服务创建时
|
|
@Override
|
|
@Override
|
|
- public void onCreate(){
|
|
|
|
|
|
+ public void onCreate() {
|
|
super.onCreate();
|
|
super.onCreate();
|
|
//服务创建时创建前台通知
|
|
//服务创建时创建前台通知
|
|
Notification notification = createForegroundNotification();
|
|
Notification notification = createForegroundNotification();
|
|
//启动前台服务
|
|
//启动前台服务
|
|
- startForeground(1,notification);
|
|
|
|
|
|
+ startForeground(1, notification);
|
|
//有业务逻辑的代码可写在onCreate下
|
|
//有业务逻辑的代码可写在onCreate下
|
|
}
|
|
}
|
|
|
|
|
|
//服务销毁时
|
|
//服务销毁时
|
|
@Override
|
|
@Override
|
|
- public void onDestroy(){
|
|
|
|
|
|
+ public void onDestroy() {
|
|
//在服务被销毁时,关闭前台服务
|
|
//在服务被销毁时,关闭前台服务
|
|
stopForeground(true);
|
|
stopForeground(true);
|
|
super.onDestroy();
|
|
super.onDestroy();
|
|
}
|
|
}
|
|
|
|
|
|
//创建前台通知,可写成方法体,也可单独写成一个类
|
|
//创建前台通知,可写成方法体,也可单独写成一个类
|
|
- private Notification createForegroundNotification(){
|
|
|
|
|
|
+ private Notification createForegroundNotification() {
|
|
//前台通知的id名,任意
|
|
//前台通知的id名,任意
|
|
String channelId = "ForegroundService";
|
|
String channelId = "ForegroundService";
|
|
//前台通知的名称,任意
|
|
//前台通知的名称,任意
|
|
@@ -59,21 +59,26 @@ public class ForegroundService extends Service {
|
|
//发送通知的等级,此处为高,根据业务情况而定
|
|
//发送通知的等级,此处为高,根据业务情况而定
|
|
int importance = NotificationManager.IMPORTANCE_HIGH;
|
|
int importance = NotificationManager.IMPORTANCE_HIGH;
|
|
//判断Android版本,不同的Android版本请求不一样,以下代码为官方写法
|
|
//判断Android版本,不同的Android版本请求不一样,以下代码为官方写法
|
|
- if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
|
|
|
|
- NotificationChannel channel = new NotificationChannel(channelId,channelName,importance);
|
|
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
+ NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
|
|
channel.setLightColor(Color.BLUE);
|
|
channel.setLightColor(Color.BLUE);
|
|
// channel.setLockscreenVisiability(Notification.VISIBILITY_PRIVATE);
|
|
// channel.setLockscreenVisiability(Notification.VISIBILITY_PRIVATE);
|
|
- NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
|
+ NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
notificationManager.createNotificationChannel(channel);
|
|
notificationManager.createNotificationChannel(channel);
|
|
}
|
|
}
|
|
|
|
|
|
//点击通知时可进入的Activity
|
|
//点击通知时可进入的Activity
|
|
Intent notificationIntent = new Intent(this, WatchMainActivity.class);
|
|
Intent notificationIntent = new Intent(this, WatchMainActivity.class);
|
|
- PendingIntent pendingIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
|
|
|
|
|
|
+ PendingIntent pendingIntent;
|
|
|
|
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
|
|
|
|
+ pendingIntent = PendingIntent.getActivity(this, 123, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
|
|
|
|
+ } else {
|
|
|
|
+ pendingIntent = PendingIntent.getActivity(this, 123, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
|
|
|
|
+ }
|
|
|
|
|
|
//最终创建的通知,以下代码为官方写法
|
|
//最终创建的通知,以下代码为官方写法
|
|
//注释部分是可扩展的参数,根据自己的功能需求添加
|
|
//注释部分是可扩展的参数,根据自己的功能需求添加
|
|
- return new NotificationCompat.Builder(this,channelId)
|
|
|
|
|
|
+ return new NotificationCompat.Builder(this, channelId)
|
|
.setContentTitle("Z+")
|
|
.setContentTitle("Z+")
|
|
.setContentText("Z+正在运行")
|
|
.setContentText("Z+正在运行")
|
|
.setContentIntent(pendingIntent)//点击通知进入Activity
|
|
.setContentIntent(pendingIntent)//点击通知进入Activity
|