我有一个开关,当 BATTERY_LOW 为 true 时会关闭 wifi:
public void getToggle(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
PackageManager pm = getPackageManager();
ComponentName compName =
new ComponentName(getApplicationContext(),
LowBatteryReceiver.class);
pm.setComponentEnabledSetting(
compName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
} else {
PackageManager pm = getPackageManager();
ComponentName compName =
new ComponentName(getApplicationContext(),
LowBatteryReceiver.class);
pm.setComponentEnabledSetting(
compName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
}
这里的wifi代码:
public class LowBatteryReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
wifiManager = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
}
}
public class OkBatteryReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
wifiManager = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
}
}
当我离开我的应用程序并再次打开它时,我如何“记住”切换的选择?我知道我必须在其中写一些东西,onResume
但onCreate
我不知道当我使用广播接收器时会发生什么。我想我必须检查一下是否COMPONENT_ENABLED_STATE_DISABLED
正确COMPONENT_ENABLED_STATE_ENABLED
?有人可以帮助我吗?谢谢