Program description:
This is a demo program on how to create a broadcast receiver and how to send a broadcast message.
Broadcast receiver: it is a component of android which responds to system wide broadcast announcements.
If any important even occurs in phone like battery low, new incoming sms, incoming call, file download etc.. then system will take that event and send it as a broadcast message to all the applications in the phone. If any application is interested in responding to that event, then that application has to register a broadcast receiver for that intent.
This application has an activity and a broadcast receiver. Activity contains a button. On clicking the button we will send a broadcast message to our receiver using explicit intent.
If you want to trigger a receiver by using implicit intent, then you can follow my tutorial on how to use implicit intent.
We use sendBroadcast() function to send a broad cast message.
Every broadcast receiver extends from class BroadcastReceiver.
To create a broadcast receiver, go to manifest file -> application tab -> scroll down -> click add button -> select receiver and give a name for your receiver. That's it, it creates a java file which contains your broadcast receiver code.
android interview questions on broadcast receivers
First activity
This is a demo program on how to create a broadcast receiver and how to send a broadcast message.
Broadcast receiver: it is a component of android which responds to system wide broadcast announcements.
If any important even occurs in phone like battery low, new incoming sms, incoming call, file download etc.. then system will take that event and send it as a broadcast message to all the applications in the phone. If any application is interested in responding to that event, then that application has to register a broadcast receiver for that intent.
This application has an activity and a broadcast receiver. Activity contains a button. On clicking the button we will send a broadcast message to our receiver using explicit intent.
If you want to trigger a receiver by using implicit intent, then you can follow my tutorial on how to use implicit intent.
We use sendBroadcast() function to send a broad cast message.
Every broadcast receiver extends from class BroadcastReceiver.
To create a broadcast receiver, go to manifest file -> application tab -> scroll down -> click add button -> select receiver and give a name for your receiver. That's it, it creates a java file which contains your broadcast receiver code.
android interview questions on broadcast receivers
First activity
package com.techpalle.b15_broadcastreceivers;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(getApplicationContext(),
MyReceiver.class);
sendBroadcast(in);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
xml file for first activity
File name : activity_main.xml
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" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
Broadcast receiver
File name : MyReceiver.java
package com.techpalle.b15_broadcastreceivers;
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) {
/*Broadcast receiver has maximum time limit of 10 seconds
*so.. don't do heavy functionalities in receiver.*/
/*1. don't show dialogs
* 2. don't do long running operations (sd card, db, internet)
* 3. don't bind to services
*/
Toast.makeText(context, "Reciever triggered..", 0).show();
}
}
Manifest file
File name : AndroidManifest.xml
package="com.techpalle.b15_broadcastreceivers"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="17" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.techpalle.b15_broadcastreceivers.MainActivity"
android:label="@string/app_name" >
Download complete code : Click to download
Interview questions on broadcast receivers:
Difference between service and broadcast receiver?
How to start an application on phone boot complete?
What is the time limit of a broadcast receiver?
How to notify user from a broadcast receiver?
Tags: android, broadcast receiver, sendbroadcast
android broadcast receiver interview questions and answers
No comments:
Post a Comment