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

}


3 comments:

  1. Excellent piece of knowledge, I had come back to read concerning your web site from my friend shiva, bangalore. I have readed atleast eight posts of your website and let me tell you, your website provides the most fascinating information. This is the knowledge that I had been craving for, I am already your rss reader currently and that I would frequently be careful for the new posts. Thanks plenty another time, Regards,Android Online Training

    ReplyDelete
  2. 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
  3. 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