我以前曾問過類似的問題,但添加WakeLock後問題得到解決。一段時間過去了,問題又回來了。活動啓動延遲(使用WakeLock)
背景 - 它是一個鬧鐘應用程序,警報活動從BroadcastReceiver(下面的代碼)開始。問題隨機出現,之前沒有問題。應用已建立的記錄器,節省了日誌數據庫 - 約我來自用戶發送日誌錯誤的所有信息 - 錯誤從來沒有發生在我...登錄:
880. 6/3 5:0:1 - == Logger initialized == from AlarmReceiver
881. 6/3 5:0:1 - sending START ALARM
882. 6/3 5:0:1 - WakeLock creating
883. 6/3 5:0:1 - WakeLock acquiring for 300000
884. 6/3 5:0:2 - START ALARM send
885. 6/3 5:0:2 - logger initialized again from StartAlarmReceiver
886. 6/3 5:0:2 - logger initialized again from Main
887. 6/3 5:0:3 - start initializeTask //from Main.onCreate
888. 6/3 5:0:3 - Main start
889. 6/3 5:0:4 - Main resume
890. 6/3 5:0:4 - Main pause
891. 6/3 5:0:4 - Main stop
... not relevant
894. 6/3 6:0:0 - logger initialized again from AlarmOnScreen create
AlarmReceiver:
public void onReceive(Context context, Intent intent) {
Logger.initialize(context, "AlarmReceiver");
if (call_is_on) {
postAgainIn5Sec();
} else {
Utils.log("sending START ALARM");
CountedWakeLock.acquireLock(context); // acquired for 30000 millis
Intent i = new Intent();
i.setAction(StartAlarmReceiver.ACTION_START_ALARM);
i.putExtras(intent.getExtras());
context.sendOrderedBroadcast(i, null);
Utils.log("START ALARM send");
}
}
StartAlarmReceiver:
public void onReceive(Context context, Intent intent) {
Logger.initialize(context, "StartAlarmReceiver");
Intent i = new Intent(context, AlarmOnScreen.class);
Bundle extras = intent.getExtras();
i.putExtras(extras);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
AlarmOnScreen(與機器人:launchMode = 「singleTask」)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.alarm_on_screen);
Logger.initialize(this, "AlarmOnScreen create");
//...
CountedWakeLock:
public synchronized static void acquireLock(Context context, long delay) {
if (sWakeLock == null) {
Utils.log("WakeLock creating");
PowerManager pm = (PowerManager) context.getApplicationContext()
.getSystemService(Context.POWER_SERVICE);
sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"AlarmReceiver lock");
sWakeLock.setReferenceCounted(false);
sLockCount = 0;
}
Utils.log("WakeLock acquiring for " + delay);
sLockCount++;
sWakeLock.acquire(delay);
}
TL; DR - 花了大約小時的AlarmOnScreen活動來調用其onCreate方法startActivity被調用後。
問題 - 爲什麼在調用startActivity之後不能啓動activity? 另外,當Alarm啓動時,我可以阻止Main啓動嗎?我從onCreate方法開始在線程中進行一些加載,並且當Main甚至不被顯示時它是毫無意義的。