Saturday, August 10, 2013

Broadcast Receiver manual On/Off

Ya Its true, you can also enable / disable Broadcast Receiver in android development.

You have to register it for enable Receiver and unregistered for disable it.One more thing whatever you want to execute in Receiver, You need to create on separate java file and also extends BroadcastReceiver.

Here is full source code and step to set Broadcast Receiver manually.


  • Create new android project. Name it: BroadcastReceiverManual
  • add below code into activity_main.xml file. 

activity_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" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="registerBroadcastReceiver"
            android:padding="15dp"
            android:text="Register"
            tools:context=".RegisterUnregister" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="unregisterBroadcastReceiver"
            android:padding="15dp"
            android:text="Unregister"
            tools:context=".RegisterUnregister" />
    </LinearLayout>

</RelativeLayout><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" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="registerBroadcastReceiver"
            android:padding="15dp"
            android:text="Register"
            tools:context=".RegisterUnregister" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="unregisterBroadcastReceiver"
            android:padding="15dp"
            android:text="Unregister"
            tools:context=".RegisterUnregister" />
    </LinearLayout>

</RelativeLayout>


  • Add below code in activity_main.java file.

activity_main.java


public class MainActivity extends Activity {

UserDefinedBroadcastReceiver broadCastReceiver = new UserDefinedBroadcastReceiver();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
    // This method enables the Broadcast receiver for
    // "android.intent.action.TIME_TICK" intent. This intent get
    // broadcasted every minute.

  public void registerBroadcastReceiver(View view) {
 
     this.registerReceiver(broadCastReceiver, new IntentFilter(
           "android.intent.action.TIME_TICK"));
     Toast.makeText(this, "Registered broadcast receiver", Toast.LENGTH_SHORT)
           .show();
  }
 
  /*
   * This method disables the Broadcast receiver
    */
  public void unregisterBroadcastReceiver(View view) {
 
     this.unregisterReceiver(broadCastReceiver);
 
     Toast.makeText(this, "unregistered broadcst receiver", Toast.LENGTH_SHORT)
           .show();
  }
}


  • Create on more file which extends BroadcastReceiver, Name it: UserDefinedBroadcastReceiver.java in project package.
  • Add below code in UserDefinedBroadcastReceiver.java file.


UserDefinedBroadcastReceiver.java



public class UserDefinedBroadcastReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
//You can do the processing here update the widget/remote views.
        StringBuilder msgStr = new StringBuilder("Current time : ");
        Format formatter = new SimpleDateFormat("hh:mm:ss a");
        msgStr.append(formatter.format(new Date()));
        Toast.makeText(context, msgStr, Toast.LENGTH_SHORT).show();
}

}


That it. Now run the project and check output.



Download Full Source Code: Download






1 comment: