Wednesday, 24 April 2013

BroadCastReceiver Demo 2

Demo:

1) MainActivity.java:
 
package com.venky.broadcastreceiverdemo2;

import android.app.Activity;
import android.content.BroadcastReceiver;
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);

            BroadcastReceiver bc = new MyBroadCastReceiver();

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

2) MyBroadCastReceiver.java:

 package com.venky.broadcastreceiverdemo2;

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

public class MyBroadCastReceiver extends BroadcastReceiver {

      @Override
      public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

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

            Toast.makeText(context, "Battery Status => " + batteryStatus+" %", 3000)
                        .show();
      }

}

3) main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>


4)Download this Project Click Here
 

1 comment:

  1. Thіs artiсlе wіll aѕsіst
    the іnteгnet viеωегs fоr setting up new ωеbpagе oг eѵen a blog
    from start to end.

    Fеel free to viѕіt my page:
    mouse click the up coming post

    ReplyDelete