简体   繁体   中英

WakeLock with PARTIAL_WAKE_LOCK is not working

I have an alarm application and was contacted by the user which reported that his alarms are sometimes delayed. I have tried different approaches and theories and finally suggested the user to use default Android's alarm as a backup.

Usually alarms were delayed by about an hour, but last time my alarm has started just after Android's one - clear suggestion, that device was asleep and got awoken with Android's alarm, allowing for mine to "continue starting" as well.

Edit: As I have modified some classes and have already received log from new version, I am changing classes and log information below.

Here's the CountedWakeLock class I am using - I created my own HandlerThread to handle delayed releasing of the wake lock, in order to check if that can affect problems with start. It did not fix the problem, but it has produced interesting information in the log (below).

public class CountedWakeLock {

private static long TIMEOUT_DEFAULT =  DateUtils.MINUTE_IN_MILLIS * 3;
private static WakeLock sWakeLock = null;
private static int sLockCount;

private static class TimeoutReleaseThread extends HandlerThread {

    static TimeoutReleaseThread mInstance = null;

    Handler mHandler;
    private Runnable mReleaseRunnable;

    public static TimeoutReleaseThread instance() {
        if (mInstance == null) mInstance = new TimeoutReleaseThread();
        return mInstance;
    }

    public TimeoutReleaseThread() {
        super("TimeoutReleaseThread HandlerThread");
        start();
        mHandler = createHandler();
        mReleaseRunnable = new Runnable() {

            @Override
            public void run() {
                Utils.log("TimeoutReleaseThread release lock");
                releaseLock();
            }
        };
    }

    private synchronized Handler createHandler() {
        return new Handler(getLooper());
    }

    public synchronized void postRelease(long timeout) {
        mHandler.removeCallbacks(mReleaseRunnable);
        mHandler.postDelayed(mReleaseRunnable, timeout);
    }
}

public synchronized static void acquireLock(Context context) {
    acquireLock(context, TIMEOUT_DEFAULT);
}

public synchronized static void acquireLock(Context context, long timeout) {
    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;
    } else if (sWakeLock.isHeld())
        Utils.log("WakeLock held already");
    else
        Utils.log("WakeLock not held");

    Utils.log("WakeLock acquiring for " + timeout);
    sLockCount++;
    sWakeLock.acquire();
    TimeoutReleaseThread.instance().postRelease(timeout);
}

public synchronized static void releaseLock() {
    Utils.log("WakeLock releasing");
    if (sWakeLock == null) {
        Utils.log("WakeLock==null");
    } else if (!sWakeLock.isHeld()) {
        Utils.log("WakeLock not held");
        sWakeLock = null;
        sLockCount = 0;
    } else {
        sLockCount--;
        if (sLockCount <= 0) {
            Utils.log("WakeLock released");
            sWakeLock.release();
            sWakeLock = null;
            if (sLockCount != 0) Utils.log("lockcount=" + sLockCount);

        }
    }
}
}

AlarmReceiver BroadcastReceiver - it is receiving intents from AlarmManager

public void onReceive(Context context, Intent intent) {
    Logger.initialize(context, "AlarmReceiver");
    if (CALL_IS_ON) {
        //set another alarm with AlarmManager to start after 5 seconds
        //doesn't happen in this situation
    } else {
        Utils.log("sending START ALARM");
        CountedWakeLock.acquireLock(context);
        Intent i = new Intent();
        i.setAction(StartAlarmReceiver.ACTION_START_ALARM);
        i.putExtras(intent.getExtras());

        context.sendOrderedBroadcast(i, null); //it is ordered, so eventual previous Alarm Activities etc. can be stopped
        Utils.log("START ALARM send");
    }
}

StartAlarmReceiver. It is actually starting the Alarm Activity

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);
    Utils.log("AlarmActivity started");
}

Updated log:

504. 27/3 5:0:0 - logger initialized again from AlarmReceiver
505. 27/3 5:0:0 - sending START ALARM
506. 27/3 5:0:0 - WakeLock creating
507. 27/3 5:0:0 - WakeLock acquiring for 180000
508. 27/3 5:0:0 - START ALARM send
509. 27/3 5:0:0 - logger initialized again from StartAlarmReceiver
510. 27/3 5:0:0 - AlarmActivity started
511. 27/3 5:0:0 - Main start
512. 27/3 5:0:1 - Main resume
513. 27/3 5:0:1 - Main pause
514. 27/3 5:0:5 - Main stop
515. 27/3 5:3:0 - TimeoutReleaseThread release lock
516. 27/3 5:3:0 - WakeLock releasing
517. 27/3 5:3:0 - WakeLock released
518. 27/3 6:46:18 - logger initialized again from AlarmOnScreen create //user said he unlocked phone then to check the time...

From the log now I believe that WakeLock is actually working - after all CPU must have been running, if TimeoutReleaseThread was able to finish working.

Question is why AlarmOnScreen Activity was not started?? And why Main Activity (lines 511-514) has started? Recently I have made AlarmOnScreen have singleTask mode set in manifest. Could that be causing the problem? But why? I need this singleTask for few other reasons...

Device is GT-S5830i with 2.3.6 Android.

I still have no idea what caused the problem, but if anyone will ever experience similar one - after many tests, it was only one simple change - adding Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED to Intent in StartAlarmReceiver.

So it's like

i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
     | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

Does it work? Yes, it does. And so far it didn't cause any problems on other phones.

Does it make sense? No, not at all.

我通过这种方式发送通知解决了我的服务问题:

startForeground(ID, notification)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM