2017-07-10 40 views
0

我想接收广播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 m使用telnet来设置模拟器的容量或'ac on/off'。

回答

0

尝试在您的清单文件中添加权限:

<uses-permission android:name="android.permission.BATTERY_STATS"/> 
+0

没有帮助。据我所知,这个许可的目的完全不同 –

相关问题