0

I have created a notification in my application to show the battery level in percentage. My manifest file is

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.create.bg"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.BATTERY_STATS"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".CreateBgActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".BatteryReceive" >
        <intent-filter>

            <action android:name="android.intent.action.BATTERY_CHANGED"></action>
        </intent-filter>
    </receiver>
</application>

I created a separate class that extends BroadcastReceiver to receive the battery level and notify in the status bar. My code is:

public class BatteryReceive extends BroadcastReceiver {
private String bat = "android.intent.action.BATTERY_CHANGED";

@Override
public void onReceive(Context context, Intent intent) {
    //Log.d("Battery Level", ""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1));
    if(intent.getAction().equals(bat)) {
    Toast.makeText(context, "Battery Level"+intent.getIntExtra("level", 0), Toast.LENGTH_SHORT).show();
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    final Notification notifyDetails = new Notification(R.drawable.ic_launcher,""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1),System.currentTimeMillis());
    //notifyDetails.flags = Notification.FLAG_AUTO_CANCEL;


    notifyDetails.setLatestEventInfo(context, "Batter Level", ""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1), null);
    mNotificationManager.notify(1, notifyDetails);
    }
}

 }

I could't get any notification or Log output for the battery level :( Is it anything wrong with my code. SOme one help me out of this.

jimmym715
  • 1,512
  • 1
  • 16
  • 25
Santhosh_pulliman
  • 2,119
  • 6
  • 30
  • 47
  • Here is the Complete Source of Battery Widget http://code.google.com/p/batterywidget/source/checkout – Rishi Feb 10 '12 at 13:08
  • @Rishi I already saw that one. The same thing i have implemented in my code, but it doesn't work. – Santhosh_pulliman Feb 10 '12 at 13:13
  • please see the below classhttp://code.google.com/p/batterywidget/source/browse/trunk/a_batterywidget/src/com/geekyouup/android/widgets/battery/BatteryWidget.java?r=5 In Remoteview Build Update You get Your battery level – Rishi Feb 11 '12 at 04:58
  • @Rishi Please correct if anything wrong in my code.... – Santhosh_pulliman Feb 11 '12 at 06:05
  • Check this answer: http://stackoverflow.com/questions/11277302/i-cant-receive-broadcast-on-battery-state-change – benchuk Apr 29 '15 at 10:27
  • ## Check this answer ## http://stackoverflow.com/questions/11277302/i-cant-receive-broadcast-on-battery-state-change – benchuk Apr 29 '15 at 10:28
  • **Check this answer:** http://stackoverflow.com/questions/11277302/i-cant-receive-broadcast-on-battery-state-change – benchuk Apr 29 '15 at 10:29

2 Answers2

0

Change:

if(intent.getAction().equals(bat))

To:

if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED))
Marvin Pinto
  • 30,138
  • 7
  • 37
  • 54
0

@Override public void onReceive(Context context, Intent intent) {

//Log.d("Battery Level", ""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1));

if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
   // This is called when Your battery is changed 

}

}

Rishi
  • 987
  • 6
  • 16