![](/img/trans.png)
[英]How to cancel alarm manager alarms when user removes the app from recent apps list?
[英]Why Alarm not fired when I remove app from recent apps?
我可以看到許多可以處理更復雜情況(例如通知)的應用程序,並且運行良好。
需求:
我只是想設置一個在特定時間觸發的警報
問題:
僅當應用程序存在於最近的應用程序中時,它才有效;如果我將其從最近的應用程序中刪除,則不會發出警報!
問題:
為什么不起作用? -我應該怎么做才能使其正常工作?
我的代碼片段:
我的警報觸發效果如下:
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), PENDING_INTENT_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
assert alarmManager != null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMilliseconds, pendingIntent);
} else {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeInMilliseconds, pendingIntent);
}
MyBroadcastReceiver是什么樣的:
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "BroadCast Fired!");
}
啟用引導:
ComponentName componentName = new ComponentName(this, MyBroadcastReceiver.class);
PackageManager packageManager = getPackageManager();
packageManager.setComponentEnabledSetting(componentName,
componentEnabledState,
PackageManager.DONT_KILL_APP);
表現:
<receiver
android:name="MyBroadcastReceiver"
android:enabled="false">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
發生這種情況的原因是,設備制造商(Oppo)添加了自定義設置,以在將應用程序從最新應用程序列表中刪除時將其殺死。
這不是標准的Android行為。 在正常的Android中,將應用程序從最新應用程序列表中刪除后,該應用程序不會被殺死(這就是為什么它可以在模擬器上正常工作的原因)。
解決方案是找到此設置在設備中的位置並進行更改。 在某些設備中,您可能還需要將應用程序固定到最新應用程序列表中,因此不會將其刪除。 但這可能會根據特定的設備型號而改變。
如果找不到設置,建議您在Oppo論壇中搜索或檢查以下頁面: https : //dontkillmyapp.com/
但是您的代碼是正確的。 該應用程序無能為力。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.