I'm trying to make an application that when a toggle button is on starts a broadcast that check if battery low and disable wifi. Instead when the toggle is off starts another broadcast and the wifi starts. The problem is that the application crashes when i press the button. this is the code for the toggle and 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 enable/disable
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);
}
}
This is the manifest. The receiver are inside <application>
and <activity>
tags:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
<application
android:allowBackup="true"
android:icon="@drawable/battery"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".MainActivity"
android:theme="@android:style/Theme.Holo.Light">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".LowBatteryReceiver"
android:enabled="false" >
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW" />
</intent-filter>
</receiver>
<receiver
android:name=".OkBatteryReceiver"
android:enabled="false" >
<intent-filter>
<action android:name="android.intent.action.BATTERY_OKAY" />
</intent-filter>
</receiver>
<activity
android:label="@string/app_name"
android:name=".about"
android:theme="@android:style/Theme.Holo.Light">
</activity>
</application>
How can i resolve? I can't find the error.. edit with logcat:
The reference from your manifest to the Receiver classes are not right. Right now, you reference them like they are a normal class within your package. Since they are inner classes, you should reference them like this (see the android:name
attribute):
<receiver
android:name=".MainActivity$OkBatteryReceiver"
android:enabled="false" >
<intent-filter>
<action android:name="android.intent.action.BATTERY_OKAY" />
</intent-filter>
</receiver>
Inner classes are referenced by the $
.