BroadcastReceiver, Service and Wakelock

无人久伴 提交于 2020-01-02 05:13:28

问题


im receiving an intent in broadcast receiver and then i start service to do more work. now what if the device is sleep and this happen, do i have to get Wakelock (AlarmManger?), and why do i need it? does my service will stop running if the device goes to sleep without getting a wakelock.


回答1:


now what if the device is sleep and this happen, do i have to get Wakelock (AlarmManger?), and why do i need it?

If the device is asleep to begin with, you will not be "receiving an intent in broadcast receiver", because the device is asleep.

do i have to get Wakelock (AlarmManger?), and why do i need it?

You don't "need it", unless you want to ensure the device stays running while you complete some work.

does my service will stop running if the device goes to sleep without getting a wakelock.

Yes.




回答2:


Looks like the Android's native WakefulBroadcastReceiver would be a perfect solution for you. Need to extend this rather than the regular BroadcastReceiver and start the service in the onReceive() in the "wakeful" manner:

startWakefulService(context, service);

and signal your work is done in the service's onHandleIntent(), calling

MyWakefulReceiver.completeWakefulIntent(intent);



回答3:


public class WakeLockManager extends BroadcastReceiver {

    private static WakeLock mWakeLock;
    private String LCLT;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Consts.WAKELOCK_INTENT)) {
            Log.v("wakelock", "GOT THE wakelock INTENT");
            boolean on = intent.getExtras().getBoolean("on");
            if (mWakeLock == null) {
                PowerManager pm = (PowerManager) context
                        .getSystemService(Context.POWER_SERVICE);
                mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                        "Breeze WakeLock");
            }
            if (on) {
                if (!mWakeLock.isHeld()) {
                    mWakeLock.acquire();
                    Log.v("wakelock", "acquiring wakelock");
                }
            } else {
                if (mWakeLock.isHeld()) {
                    Log.v("wakelock", "releasing wakelock");
                    mWakeLock.release();
                }
                mWakeLock = null;
            }
        }
    }
}

look at the above code ..put it in a separate class file and and in your manifest define it for some custom intent .... now that this class will respond to a custom intent ...just broadcast that intent and you can turn the wakelock on or off in your entire app since the wakelock is static..like this :

public void setWakeup(boolean status) {
    Intent wakelock_Intent = new Intent(CUSTOM_INTENT);
    wakelock_Intent.putExtra("on", status);
    this.sendBroadcast(wakelock_Intent);
}

the above would be defined in your alarmmanager code so it schedules a call



来源:https://stackoverflow.com/questions/5681071/broadcastreceiver-service-and-wakelock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!