標籤:綁定 inner pre 源碼 int start tno bsp gray 詳細說明
本來打算對Notification進行詳細說明 和源碼解讀的 發現寫的時刻又不想寫。
Notification 保活
算了 直接上原始碼
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT < 18) {
startForeground(GRAY_SERVICE_ID, new Notification());
} else {
Intent innerIntent = new Intent(context, AuxiliaryService.class);
startService(innerIntent);
Notification notification = new Notification();
startForeground(GRAY_SERVICE_ID, notification);
}
return super.onStartCommand(intent, flags, startId);
}
/**
* 輔助Service 用來開啟一個通知 就結束 不要建立內部類 不然回收有問題
*/
public class AuxiliaryService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startNotification();
return super.onStartCommand(intent, flags, startId);
}
/** 啟動通知*/
private void startNotification(){
Notification notification = new Notification();
this.startForeground(MQTTService.GRAY_SERVICE_ID, notification);
stopSelf(); //關鍵 如果AuxiliaryService 沒有與什麼組件綁定 系統就會回收
stopForeground(true);
}
}
Android 進程保活 Notification詳細說明