0

I want to receive broadcast BATTERY_LOW, so I declared broadcast receiver in manifest (in order not to rely on current activity. I want my application receive this broadcast even when it's not running. Here's what I did.

<?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>

And BatteryLevelReceiver itself (package 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));
}
}

I cant receive any of declared events!There are some similar questions here, but none of them helps me. Im using telnet to set capacity or 'ac on/off' for an emulator.

Roman Diachuk
  • 31
  • 1
  • 4

1 Answers1

0

Try to add permission in your manifest file:

<uses-permission android:name="android.permission.BATTERY_STATS"/>
e.rikov
  • 46
  • 3