Overview
The Android system uses broadcasts messages (often are called Intents or Events) to transfer information to applications. The Broadcasts can be sent either by Android apps or, most commonly, by the system itself. There are many system generated events that Intent class defines for receiving broadcasts, which some of them you can find in the Standard Broadcast Actions of Android Content. In this tutorial we are going to show you step by step how to create a BroadcastReceiver that receives a custom Intent, which contains data that the user gave from the screen.
Example Program for BroadCastReceiver
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"
tools:context=".MainActivity" >
<EditText android:id="@+id/extraIntent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/sendMessage" />
<Button
android:id="@+id/btnStartBroadcast"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/extraIntent"
android:onClick="broadcastCustomIntent"
android:text="@string/myBroadcastIntent" />
</RelativeLayout>
MainActivity.Java
package androiindians.broadcastreciver;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void broadcastCustomIntent(View view)
{
Intent intent = new Intent("MyCustomIntent");
EditText et = (EditText)findViewById(R.id.extraIntent);
// add data to the Intent
intent.putExtra("message", (CharSequence)et.getText().toString());
intent.setAction("androiindians.broadcastreciver.A_CUSTOM_INTENT");
sendBroadcast(intent);
}
}
MyBroadcastReceiver.java
package androiindians.broadcastreciver;
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
// Extract data included in the Intent
CharSequence intentData = intent.getCharSequenceExtra("message");
Toast.makeText(context, "Raj received the Intent's message: "+intentData, Toast.LENGTH_LONG).show();
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="androiindians.broadcastreciver">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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="MyBroadcastReceiver">
<intent-filter>
<action android:name="androiindians.broadcastreciver.A_CUSTOM_INTENT">
</action>
</intent-filter>
</receiver>
</application>
</manifest>
Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">BroadcastReceiversTest</string>
<string name="action_settings">Settings</string>
<string name="sendMessage">Write a message to broadcast!</string>
<string name="myBroadcastIntent">Broadcast an Intent now...</string>
</resources>