Monday, 19 August 2013

Battery BroadcastReceiver in Android

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));
}

}


2 comments:

  1. I also maintain a blog for tutorials I completely appreciate
    your style of presentation should be very use full for not only beginners but also to experts

    ReplyDelete
  2. Hey it was really a nice article i was really impressed by reading this article on Android Online Training, In Hyderabad 123trainings also provide the best Android Online Training with an experienced faculty.

    ReplyDelete