![](/img/trans.png)
[英]Local Notification after App killed using Alarm manager in android 10
[英]My alarm manager killed after my app killed in android
我有一個AlarmManager
每10分鍾顯示一次Toast
但是當操作系統在后台殺死應用程序時,我的AlarmManager
不再起作用。 我該怎么辦?
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context con, Intent arg1) {
Global.ShowMessage(con, Global.GetCurrentDateTime());
}
}
在Manifest.xml
:
<receiver android:name=".MyReceiver" > </receiver>
在主要Activity
:
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
long time = cal.getTimeInMillis();
Intent i = new Intent(this, MyReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 9854, i, PendingIntent.FLAG_UPDATE_CURRENT);
// am.set(AlarmManager.RTC_WAKEUP,time,pi);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, time, 600 * 1000, pi);
系統永遠不會直接殺死活動。 相反,它殺死了活動在其中運行的進程,不僅破壞了活動,而且還破壞了該進程中運行的所有其他事物。
但是您可以創建一個不會被Activity殺死的Service
。 更具體地說,您希望服務在前台運行,而不會被服務文檔中所述的系統殺死。
對於特定示例,我更願意讓Google指導您使用此[example]( https://developer.android.com/reference/android/app/Service.html#startForeground ( int ,android.app.Notification))。
將此代碼追加到清單中。
<receiver android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.