我之前也问过类似的问题,但是添加 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
报警接收器:
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");
}
}
启动警报接收器:
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(使用 android:launchMode="singleTask")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.alarm_on_screen);
Logger.initialize(this, "AlarmOnScreen create");
//...
计数唤醒锁:
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 - 在调用 startActivity 之后,AlarmOnScreen Activity 调用其 onCreate 方法大约需要一个小时。
问题 - 为什么在调用 startActivity 后活动没有开始?另外,我可以在启动警报时阻止 Main 启动吗?我在从 onCreate 方法开始的线程中做了一些加载,这是毫无意义的,当 Main 甚至不应该显示时。