【发布时间】:2010-12-23 22:24:43
【问题描述】:
我试图让我的数字时钟小部件的 UpdateService 在屏幕关闭时停止以节省电池,然后在屏幕激活时重新打开。我目前在 AppWidgetProvider 的 onReceive() 中有它,但我也在 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;
}
有人可以帮我吗?我被难住了。
【问题讨论】:
-
您面临的问题是什么?
-
您的代码不是在屏幕关闭时启动服务,并在屏幕打开时停止服务 - 与您所说的想要做的相反吗?