use*_*143 7 android android-manifest android-emulator
我正在尝试在屏幕关闭(锁定)时关闭WiFi,并在屏幕打开(解锁)时再次打开它.
我做了一个BroadcastReceiver; 放入清单这段代码:
<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>
Run Code Online (Sandbox Code Playgroud)
这是班级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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
其结果是-既ACTION_SCREEN_ON
和ACTION_SCREEN_OFF从未被解雇了!
USER_PRESENT并且BOOT_COMPLETED工作得很好,但其他没有.我正在使用模拟器,而不是真正的设备 - 这会导致问题吗?
有帮助吗?我需要打开和关闭屏幕以启用/禁用WiFi以节省电池.
提前致谢
PoO*_*oOk 13
要捕获SCREEN_OFF和SCREEN_ON操作(可能还有其他操作),您必须按代码配置BroadcastReceiver,而不是通过清单.
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenStateBroadcastReceiver();
registerReceiver(mReceiver, intentFilter);
Run Code Online (Sandbox Code Playgroud)
它经过测试并且运行正常.
你无法通过XML捕获这些意图(我忘了为什么).但是,您可以使用a Service注册其中的BroadcastReceiver成员并在其中onStartCommand()注销它onDestroy().这将要求服务在后台运行,不断地或只要您需要它,所以一定要探索替代路线.
你可以BroadcastReceiver在你的Service类中定义如下:
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于一个稍微复杂的示例,但是一个显示BroadcastReceiver和Service交互的示例从我的应用程序,ElectricSleep 看到CheckForScreenBugAccelerometerService.
| 归档时间: |
|
| 查看次数: |
10358 次 |
| 最近记录: |