活动开始延迟(使用WakeLock)

时间:2013-03-19 15:49:40

标签: android android-activity wakelock start-activity

之前我曾问过类似的问题,但是在添加WakeLock之后问题就解决了。一段时间过去了,问题又回来了。

后台 - 它是一个警报应用程序,从BroadcastReceiver启动警报活动(下面的代码)。问题已经开始随机发生,以前没有问题。应用程序已在Logger中构建,将日志保存到数据库 - 所有关于我来自用户发送的日志的错误的信息 - 错误从未发生在我身上...日志:

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(使用android: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 - 在调用startActivity之后,AlarmOnScreen活动花了大约一小时来调用它的onCreate方法。

问题 - 为什么活动在调用startActivity后才开始? 此外,我可以在启动闹钟时阻止Main启动吗?我在onCreate方法启动的线程中做了一些加载,当Main甚至不显示时,它是毫无意义的。

1 个答案:

答案 0 :(得分:0)

下一个日志已经澄清问题是由WakeLock无法正常工作引起的 - 详情请转到WakeLock with PARTIAL_WAKE_LOCK is not working