Introduction
Broadcast Receiver is one of the main components of the Android operating system. Broad cast receivers respond to broad cast messages from other applications or from the operating system itself. The Android operating system will broad cast messages to all the registered receivers whenever an event occurs in the system. Some of the Broadcast the Android system has is as follows.
- android.intent.action.BATTERY_CHANGED
- android.intent.action.BATTERY_LOW
- android.intent.action.BOOT_COMPLETED
- android.intent.action.CALL
- etc
- <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">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:id="@+id/broadCastButton"
- android:text="Send BroadCast"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:text="Broad Cast Sample"
- android:id="@+id/textView"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true" />
- </RelativeLayout>
- Button button = (Button) findViewById(R.id.broadCastButton);
- public class MyBroadCastListner extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- Toast.makeText(context,"Broad cast received",Toast.LENGTH_SHORT).show();
- }
- }
- <receiver android:name=".MyBroadCastListner">
- <intent-filter>
- <action android:name="com.negociosit.CUSTOM_INTENT"></action>
- </intent-filter>
- </receiver>
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent = new Intent();
- intent.setAction("com.negociosit.CUSTOM_INTENT");
- sendBroadcast(intent);
- }
- });

kalu singh raoPosted May 9, 2016, 12:43 PM
NIce...
Kuppurasu NagarajPosted May 9, 2016, 11:58 AM
Nice Sharing..
Debasis SahaPosted May 9, 2016, 8:37 AM
Good One..