Battery BroadcastReceiver in Android

August 19, 2013
BatteryBroadCast.java:

package com.ram.batteybroadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class BatteryBroadCast extends BroadcastReceiver {

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

int bStatus = intent.getIntExtra("level", 0);

Toast.makeText(c, "Battery Status :" + bStatus, Toast.LENGTH_LONG)
.show();

}

}


MainActivity.java:

package com.ram.batteybroadcast;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

registerReceiver(new BatteryBroadCast(), new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
}

}


Artikel Terkait

Previous
Next Post »