问题描述
我正在尝试在屏幕关闭(锁定)时关闭 WiFi,并在屏幕打开(解锁)时再次打开.
I'm trying to turn WiFi off when the screen is OFF (locked), and turn it on again when screen is ON (unlocked).
我做了一个BroadcastReceiver
;将此代码放入清单中:
I made a BroadcastReceiver
; put in manifest this code:
<receiver android:name="MyIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.USER_PRESENT" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
这是类MyIntentReceiver
:
package org.androidpeople.boot;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyIntentReceiver extends BroadcastReceiver {
// Called when boot completes
public static boolean startup;
@Override
public void onReceive(Context context, Intent intent) {
// Set what activity should launch after boot completes
System.out.println("Intent Action: " + intent.getAction());
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
System.out.println("locked : ACTION_SCREEN_OFF");
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
System.out.println("not locked : ACTION_SCREEN_ON ");
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
System.out.println("User Unlocking it ");
}
else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
// this to indicate that program is running
// automaticlly not manually by user
startup = true;
System.out.println("Automatic BOOT at StartUp");
Intent startupBootIntent = new Intent(context, LaunchActivity.class);
startupBootIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startupBootIntent);
}
}
}
结果是 - ACTION_SCREEN_ON
ACTION_SCREEN_OFF
从未被解雇!USER_PRESENT
和 BOOT_COMPLETED
工作正常,但另一个没有.我使用的是模拟器,而不是真实设备 - 这会导致问题吗?
And the result is - both ACTION_SCREEN_ON
and ACTION_SCREEN_OFF
never fired!USER_PRESENT
and BOOT_COMPLETED
worked fine but the other did not. I'm using an emulator, not a real device - can this cause the problem?
有什么帮助吗?我需要打开和关闭屏幕才能启用/禁用 WiFi以节省电池.
Any help?I need to catch the screen on and off in order to enable/disable WiFito save battery.
提前致谢
推荐答案
你无法通过 XML 捕捉到这些意图(我忘记了为什么).但是,您可以使用 Service
在其 onStartCommand()
中注册一个 BroadcastReceiver
成员并在其 onDestroy().这将要求服务在后台运行,持续或只要您需要它,所以一定要探索替代路线.
You cannot catch those intents through XML (I forget why). However, you could use a
Service
that registers a BroadcastReceiver
member in its onStartCommand()
and unregisters it in its onDestroy()
. This would require the service to be running in the background, constantly or as long as you need it to, so be sure to explore alternative routes.
您可以像这样在
Service
类中定义 BroadcastReceiver
:
You could define the
BroadcastReceiver
in your Service
class like so:
private final class ScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//stuff
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
//other stuff
}
}
}
对于一个稍微复杂但显示
BroadcastReceiver
和 Service
如何交互的示例,请参见 CheckForScreenBugAccelerometerService 来自我的应用 ElectricSleep.
For a slightly complicated example, but one that shows how the
BroadcastReceiver
and Service
interact see CheckForScreenBugAccelerometerService from my app, ElectricSleep.
这篇关于ACTION_SCREEN_ON 和 ACTION_SCREEN_OFF 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!