ACTION_SCREEN_OFF 时如何停止服务 [英] How to stop service when ACTION_SCREEN_OFF

查看:57
本文介绍了ACTION_SCREEN_OFF 时如何停止服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的数字时钟小部件的 UpdateService 在屏幕关闭时停止以节省电池电量,然后在屏幕激活时重新打开.我目前在 AppWidgetProvider 的 onReceive() 中有它,但我也在 BroadcastReciever 中尝试过.

I am trying to make my UpdateService for my digital clock widget stop when the screen is turned off to conserve battery, and then back on when the screen is activated. I currently have it in my onReceive() in my AppWidgetProvider, but I have also tried it in a BroadcastReciever.

我当前的代码是:

public static boolean wasScreenOn = true;

public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
    Log.d("Screen switched on. ", "Starting DigiClock UpdateService.");
    context.startService(new Intent(UpdateService2by2.ACTION_UPDATE));
    wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
    Log.d("Screen switched off. ", "Stopping DigiClock UpdateService.");
    context.stopService(new Intent(context, UpdateService2by2.class));
    wasScreenOn = true;
}

有人可以帮我吗?我被难住了.

Can anyone help me out here? I am stumped.

推荐答案

我相当确定您必须在 ACTION_SCREEN_OFF/ON 的代码中注册您的接收器.我不认为在清单中注册它们会起作用.

I'm fairly sure that you have to register your receiver in code for ACTION_SCREEN_OFF/ON. I don't think registering them in the manifest will work.

这篇关于ACTION_SCREEN_OFF 时如何停止服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆