啓動粘性服務。在用戶(強制)從最近列表中刪除應用程序時關閉應用程序後立即重新啓動該服務。服務也是在啓動設備後直接啓動的,所以粘性通知永遠不會消失。請記住,粘性通知永不消失可能會使一些用戶感到不安。
OngoingNotificationService.class:
public class OngoingNotificationService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return Service.START_STICKY;
}
@Override
public void onCreate() {
// Check if notification should be shown and do so if needed
}
}
OngoingNotificationServiceStarter.class:
public class OngoingNotificationServiceStarter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, OngoingNotificationService.class);
context.startService(i);
}
}
的AndroidManifest.xml:
<manifest>
...
<application>
...
<service android:name=".OngoingNotificationService" />
<receiver android:name=".OngoingNotificationServiceStarterr">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
你能有一個服務在後臺運行? – Eenvincible