我正在使用廣播接收器的報警器,當指定的時間發生時,我需要用額外的參數更新MainActivity,但它的行爲是隨機的。沒有從廣播接收器調用活動
這裏是在廣播接收機
public void onReceive(Context context, Intent intent){
//calculate time
//check time
if(isTime){
Log.d("tag", "sleep");
//set value in shared preference
editor = (context.getSharedPreferences("uniqueId", 0)).edit();
editor.putBoolean("sleepmode",false);
editor.apply();
Intent gotoSmileyScreen = new Intent(context, MainActivty.class);
gotoSmileyScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(gotoSmileyScreen);
}
}
在logcat中,我能夠看到「休眠」的代碼,所以我知道該方法被調用在正確的時間
這裏是代碼MainActivity
//inside on create
//get value from shared preference and check
if(!isSleepMode){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}else{
//do nothing
}
用戶將最有可能已經在活動,但也不一定。 正在爲某些設備調用該活動,並按預期關閉屏幕,但對於其他設備而言,活動永遠不會從意圖中調用,並且屏幕也不會關閉。
從文檔,Intent.FLAG_ACTIVITY_CLEAR_TASK,應該再次調用它之前清除活動還是我失去了一些東西?
有沒有更好的方法來更新來自接收器的活動而無需再次調用它?
編輯:我的應用程序是一個啓動器,它以任何方式影響調用的意圖?