我正在尝试在屏幕关闭(锁定)时关闭WiFi,并在屏幕开启(解锁)时再次打开它.
我做了一个BroadcastReceiver;放入清单这段代码:
这是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工作正常,但另一个没有.我正在使用模拟器,而不是真正的设备 – 这会导致问题吗?
有帮助吗?
我需要打开和关闭屏幕才能启用/禁用WiFi
节省电池.
提前致谢