广播接收器、服务和唤醒锁

发布于 2024-11-01 10:26:29 字数 129 浏览 8 评论 0原文

我在广播接收器中收到意图,然后我开始服务以完成更多工作。现在,如果设备处于睡眠状态并且发生这种情况怎么办,我是否必须获得唤醒锁(AlarmManger?),为什么我需要它? 如果设备在没有获得唤醒锁的情况下进入睡眠状态,我的服务是否会停止运行。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉城メ夜风 2024-11-08 10:26:29

现在,如果设备处于睡眠状态并且发生这种情况怎么办,我是否必须获得 Wakelock(AlarmManger?),为什么需要它?

如果设备一开始就处于睡眠状态,您将不会“在广播接收器中接收意图”,因为设备处于睡眠状态。

我是否必须获得 Wakelock(AlarmManger?),为什么需要它?

您并不“需要它”,除非您想确保设备在完成某些工作时保持运行。

如果设备在没有获得唤醒锁的情况下进入睡眠状态,我的服务是否会停止运行。

是的。

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.

独自唱情﹋歌 2024-11-08 10:26:29

看起来 Android 原生的 WakefulBroadcastReceiver 将是一个完美的解决方案为你。需要扩展它而不是常规的 BroadcastReceiver 并以“唤醒”方式在 onReceive() 中启动服务:

startWakefulService(context, service);

并表明您的工作已在服务的 >onHandleIntent(),调用

MyWakefulReceiver.completeWakefulIntent(intent);

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);
陌上青苔 2024-11-08 10:26:29
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;
            }
        }
    }
}

查看上面的代码..将其放入单独的类文件中,并在清单中为某些自定义意图定义它....现在该类将响应自定义意图...只需广播该意图即可由于唤醒锁是静态的,因此整个应用程序中的唤醒锁打开或关闭。就像这样:

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

上面的内容将在您的 Alarmmanager 代码中定义,以便安排调用

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文