0
我在我的應用程序中有一項服務,它是一個醫藥應用程序,服務應該在整個應用程序安裝過程中運行而不會被殺死。 所以我嘗試了幾種方法,嘗試使用返回START_STICKY
的常規服務,但沒有按照應該的原則重新創建服務。然後我決定嘗試使用前臺服務。 我創造它贏得了服務onStartCommand這樣Android前臺服務檢查是否已創建
startForeground(FOREGROUND_ID, buildForegroundNotification());
return START_STICKY;
}
private Notification buildForegroundNotification() {
NotificationCompat.Builder b = new NotificationCompat.Builder(this);
b.setOngoing(true);
b.setContentTitle("My App");
b.setContentText("Therapy is running");
b.setSmallIcon(R.drawable.ic_launcher);
return (b.build());
}
所以我有兩個問題: - 可前臺服務可以由Android殺? - 如何檢查通知是否已創建,因此每次啓動服務時都不會創建該通知(由於活動正在與之通信,因此該活動也在運行時多次重新創建服務)