wxl_branch
parent
c021903047
commit
38fc24aecf
@ -0,0 +1,73 @@
|
||||
package com.example.sleep.service;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.os.IBinder;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
|
||||
import com.example.sleep.R;
|
||||
|
||||
/**
|
||||
* 用于显示通知的Service
|
||||
*/
|
||||
public class DaemonService extends Service {
|
||||
public static final int NOTICE_ID = 100;
|
||||
private NotificationManager manager;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化获取channel manager与notification构造器
|
||||
*/
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||
Notification.Builder sleep = createNotification();
|
||||
manager.notify(NOTICE_ID, sleep.build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建通知及通知频道
|
||||
*
|
||||
* @return 通知构造器
|
||||
*/
|
||||
Notification.Builder createNotification() {
|
||||
Notification.Builder builder = new Notification.Builder(this);
|
||||
builder.setSmallIcon(R.drawable.sleep_1)
|
||||
.setContentTitle("睡眠助手")
|
||||
.setContentText("正在记录您的睡眠...")
|
||||
.setOngoing(true)
|
||||
.setDefaults(NotificationCompat.DEFAULT_ALL);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
NotificationChannel channel = new NotificationChannel("1", "睡眠记录", NotificationManager.IMPORTANCE_LOW);
|
||||
channel.canBypassDnd();
|
||||
channel.getAudioAttributes();
|
||||
channel.setBypassDnd(true);
|
||||
channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
|
||||
manager.createNotificationChannel(channel);
|
||||
builder.setChannelId("1");
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
// 取消通知
|
||||
NotificationManager mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||
if (mManager != null) {
|
||||
mManager.cancel(NOTICE_ID);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.example.sleep.service;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.os.IBinder;
|
||||
|
||||
public class GrayService extends Service {
|
||||
|
||||
private final static int GRAY_SERVICE_ID = -1001;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 利用曾经的漏洞保活
|
||||
*/
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
startForeground(GRAY_SERVICE_ID, new Notification());
|
||||
//stopForeground(true);
|
||||
stopSelf();
|
||||
return super.onStartCommand(intent, flags, startId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
throw new UnsupportedOperationException("Not yet implemented");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue