我想接收广播BATTERY_LOW,所以我在清单中声明了广播接收器(为了不依赖于当前的活动。我希望我的应用程序即使在它没有运行时接收这个广播。这就是我做的。
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name=".BatteryLevelReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW"/>
<action android:name="android.intent.action.BATTERY_OKAY"/>
<action android:name="android.intent.action.POWER_CONNECTED"/>
<action android:name="android.intent.action.POWER_DISCONNECTED"/>A
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
而BatteryLevelReceiver本身(包com.wayruha.serviceguard.onduty):
public class BatteryLevelReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.w(this.getClass().getName(),intent.getAction());
Log.w(this.getClass().getName(),"Low level:"+intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1));
}
}
我可以t receive any of declared events!There are some similar questions here, but none of them helps me. I
使用telnet为模拟器设置容量或'ac on / off'。
答案 0 :(得分:0)
尝试在清单文件中添加权限:
<uses-permission android:name="android.permission.BATTERY_STATS"/>