Thursday, 12 February 2015

Battery BroadcastReceiver Example



ScreenShots:


BatteryBroadCast.java

package com.swamys.batterybroadcast;

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 context, Intent intent) {
              int battery_status = intent.getIntExtra("level", 0);
              Toast.makeText(context, "" + battery_status, Toast.LENGTH_LONG).show();
       }
}


MainActivity.java

package com.swamys.batterybroadcast;

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: