Tuesday, 3 December 2013

Custom BroadcastReceiver in Android

This example will show you how to create our own custom BroadCastReceiver in Android.

ScreenShots:


strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">CustomBroadcastReceiver</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="textview_title">Custom BroadCast Receiver example</string>
    <string name="button_title">Custom BroadCast</string>

</resources>

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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="108dp"
        android:text="@string/button_title" 
        android:onClick="customBroadCast"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:text="@string/textview_title"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

CustomBroadCast.java:
package com.ram.custombroadcastreceiver;

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

public class CustomBroadCast extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
 
Toast.makeText(context, "BroadCast Detected", Toast.LENGTH_LONG).show();
}

}

MainActivity.java:
package com.ram.custombroadcastreceiver;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

registerReceiver(new CustomBroadCast(), new IntentFilter(
"com.ram.CUSTOM_BROADCAST"));
}

public void customBroadCast(View v) {

Intent intent = new Intent();

intent.setAction("com.ram.CUSTOM_BROADCAST");

sendBroadcast(intent);
}
}

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ram.custombroadcastreceiver"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.ram.custombroadcastreceiver.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!--
        <receiver android:name="CustomBroadCast" >
            <intent-filter>
                <action android:name="com.ram.CUSTOM_BROADCAST" >
                </action>
            </intent-filter>
        </receiver>
        -->
    </application>

</manifest>