Broadcast Receivers simply respond to broadcast messages from other
applications or from the system itself. These messages are also called as events
or intents.
There are following two important steps to make BroadcastReceiver
works for the system broadcasted intents −
·
Creating the Broadcast Receiver.
·
Registering Broadcast Receiver
A broadcast receiver is implemented as a subclass of BroadcastReceiver class and overriding the onReceive()
method where each message is received as a Intent object parameter.
Registering Broadcast Receiver
An application
listens for specific broadcast intents by registering a broadcast receiver
in AndroidManifest.xml file. Consider we are going to
register MyReceiver for system generated event
ACTION_BOOT_COMPLETED which is fired by the system once the Android system has
completed the boot process.
BROADCAST-RECEIVER
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <receiver android:name="MyReceiver">
<intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"> </action> </intent-filter>
</receiver></application>
Now whenever your Android device gets booted, it will be intercepted by
BroadcastReceiver MyReceiver and implemented logic inside onReceive() will be executed.
Following is the content of the modified main activity file MainActivity.java. This file
can include each of the fundamental life cycle methods. We have added broadcastIntent() method to broadcast a custom intent.
package com.example.myapplication; import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View; public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
public void broadcastIntent(View view){ Intent intent = new Intent(); intent.setAction("com.example.CUSTOM_INTENT"); sendBroadcast(intent); }}
Following is the content of MyReceiver.java:
package com.example.myapplication; import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show(); }}
Following will the modified content of AndroidManifest.xml file. Here we have added
<receiver.../> tag to include our service:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
<receiver android:name="MyReceiver"> <intent-filter> <action android:name="com.example.CUSTOM_INTENT"> </action> </intent-filter> </receiver> </application> </manifest>
Following will be the content of res/layout/activity_main.xml file to include a button to broadcast
our custom intent −
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Example of Broadcast" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="30dp" />
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials point " android:textColor="#ff87ff09" android:textSize="30dp" android:layout_above="@+id/imageButton" android:layout_centerHorizontal="true" android:layout_marginBottom="40dp" />
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton" android:src="@drawable/abc" android:layout_centerVertical="true" android:layout_centerHorizontal="true" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:text="Broadcast Intent" android:onClick="broadcastIntent" android:layout_below="@+id/imageButton" android:layout_centerHorizontal="true" /> </RelativeLayout>
You can try implementing other BroadcastReceiver to intercept system
generated intents like system boot up, date changed, low battery etc.
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <receiver android:name="MyReceiver">
<intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"> </action> </intent-filter>
</receiver></application>
<intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"> </action> </intent-filter>
</receiver></application>
package com.example.myapplication; import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View; public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
public void broadcastIntent(View view){ Intent intent = new Intent(); intent.setAction("com.example.CUSTOM_INTENT"); sendBroadcast(intent); }}
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
public void broadcastIntent(View view){ Intent intent = new Intent(); intent.setAction("com.example.CUSTOM_INTENT"); sendBroadcast(intent); }}
package com.example.myapplication; import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show(); }}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
<receiver android:name="MyReceiver"> <intent-filter> <action android:name="com.example.CUSTOM_INTENT"> </action> </intent-filter> </receiver> </application> </manifest>
<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
<receiver android:name="MyReceiver"> <intent-filter> <action android:name="com.example.CUSTOM_INTENT"> </action> </intent-filter> </receiver> </application> </manifest>
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Example of Broadcast" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="30dp" />
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials point " android:textColor="#ff87ff09" android:textSize="30dp" android:layout_above="@+id/imageButton" android:layout_centerHorizontal="true" android:layout_marginBottom="40dp" />
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton" android:src="@drawable/abc" android:layout_centerVertical="true" android:layout_centerHorizontal="true" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:text="Broadcast Intent" android:onClick="broadcastIntent" android:layout_below="@+id/imageButton" android:layout_centerHorizontal="true" /> </RelativeLayout>
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Example of Broadcast" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="30dp" />
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials point " android:textColor="#ff87ff09" android:textSize="30dp" android:layout_above="@+id/imageButton" android:layout_centerHorizontal="true" android:layout_marginBottom="40dp" />
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton" android:src="@drawable/abc" android:layout_centerVertical="true" android:layout_centerHorizontal="true" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:text="Broadcast Intent" android:onClick="broadcastIntent" android:layout_below="@+id/imageButton" android:layout_centerHorizontal="true" /> </RelativeLayout>
Comments
Post a Comment