Technical Knowledge

technical knowledge is that type of blogger in which you can find all the soluctions of your problems which you cannot solve you can see the video given on this blog and solve your problem by yourself.

Like us on Facebook

Technical Knowledge
Showing posts with label receiver. Show all posts
Showing posts with label receiver. Show all posts

Sunday, March 29, 2015

Sticky broadcast receiver assignment: android tutorial

Program description:

This is an assignment program on using sticky broadcast receiver. This assignment has 2 applications. 

First application will be having one activity with one button. This activity also has a dynamic receiver which will be registered on clicking that button. 
Second application is also having one activity with one button. A sticky broadcast will be fired on clicking that button.

First user will open second application and clicks on the button to send a sticky broadcast to send a broadcast with action "com.techpalle.action.STICKY".

Then user will open first application's activity and click the button to register for receiver for an action "com.techpalle.action.STICKY". Once user clicks on the button to register dynamic receiver with an action which was previously broadcasted by second application, android will immediately call the onReceive() method of the current application's dynamic receiver. Reason for this is, sticky broadcasts will stick with android for future users.

Wanted to know more about what exactly is a sticky broadcast?

First Application's First activity:

package com.techpalle.b15_rcvr_asg1;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

private BroadcastReceiver br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Triggered..", 0).show();
}
};
private IntentFilter inf;
Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inf = new IntentFilter();
inf.addAction("com.techpalle.action.STICKY");

b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
registerReceiver(br, inf);
}
});
}

@Override
protected void onDestroy() {
unregisterReceiver(br);
super.onDestroy();
}

}

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" >

Manifest file
File name : AndroidManifest.xml

  
package="com.techpalle.b15_rcvr_asg1"
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_rcvr_asg1.MainActivity"
android:label="@string/app_name" >








Second Application's First activity:

package com.techpalle.b15_rcvr_asg1_app2;

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();
in.setAction("com.techpalle.action.STICKY");
sendStickyBroadcast(in);
}
});
}

}

xml file for first activity (second application)
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" />

Manifest file (for second application)
File name : AndroidManifest.xml

  
package="com.techpalle.b15_rcvr_asg1_app2"
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_rcvr_asg1_app2.MainActivity"
android:label="@string/app_name" >









Download complete code : Click to download

Android Interview questions related to Sticky broadcast receivers:
Difference between broadcast, sendorderedbroadcast, and sendstickybroadcast?
How to send battery low broadcast?
Difference between Intent, Sticky Intent, and Pending Intent?

Tags: android, sticky broadcast, receivers, sticky, dynamic receivers, sticky intent, permission, BROADCAST_STICKY.

Broadcast receiver with implicit intent: Android tutorial

Program description:

This is one more program on how to create a broadcast receiver and how to send a broadcast message by using implicit intent.


This program has an activity, and 2 receivers. Activity has one button. On clicking the button an implicit intent will be used to send a broadcast with action com.techpalle.action.RECEIVER1.
This will trigger Receiver1. In Reciever1 we will send one more broadcast with an implicit intent with action com.techpalle.action.RECEIVER2. While sending this broadcast we will generate a random number between 0 to 100, and send that number as extra information in the intent.
This will trigger Receiver2, which displays random number passed from first broadcast receiver.

This program also has a demo of how to send a broadcast from one broadcast receiver to other broadcast receiver, by using implicit intent.

First Activity:

package com.techpalle.b15_asg_receivers;

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) {
// TODO Auto-generated method stub
Intent in = new Intent();
in.setAction("com.techpalle.action.RECEIVER1");
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 : Receiver1.java

package com.techpalle.b15_asg_receivers;

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

public class Receiver1 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "first app..receiver1", 0).show();
Intent in = new Intent();
in.setAction("com.techpalle.action.RECEIVER2");
int ran = (int) (Math.random()*100);
in.putExtra("ran", ran);
context.sendBroadcast(in);
}
}

Broadcast receiver
File name : Receiver2.java

package com.techpalle.b15_asg_receivers;

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

public class Receiver2 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int ran = intent.getExtras().getInt("ran");
Toast.makeText(context, "First app..recvr2..ran.."+ran, 0).show();
}
}

Manifest file
File name : AndroidManifest.xml


  
package="com.techpalle.b15_asg_receivers"
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_asg_receivers.MainActivity"
android:label="@string/app_name" >


















Download complete code : Click to download

Interview questions on broadcast receivers:
Broadcast receiver runs in which thread?
Is it possible to bind a service from receiver?

Tags: android, broadcast receiver, sendbroadcast, implicit intent

Saturday, March 28, 2015

Ordered broadcast receiver assignment: android tutorial

Program description:

This is an assignment program on using ordered broadcast receiver. This assignment has 2 applications. 


Assignment description: 
First application is having one activity, and 2 broadcast receivers (static).
Both the receivers have an intent filter with action "com.techpalle.action.ORDERED".
But Receiver1 has android:priority="1", and Receiver2 has android:priority="3".
In this first application first activity has a button to send ordered broadcast with action "com.techpalle.action.ORDERED". 

As you already know there are 3 types of broadcast receivers are there in android.

Note: More the priority number, higher the priority. Higher priority receivers will be executed first.

Second application has one activity, and 1 broadcast receiver (static).
This receiver has intent filter with same action "com.techpalle.action.ORDERED".
This Receiver3 has android:priority="2".

Assumption: It is assumed that the second application should be already in the phone or emulator. So make sure that you first run the second application and then first application.

Now once user runs the first application, and clicks the button on the first activity it will send an ordered broadcast. Now there are 3 receivers are matching with the intent action in their intent-filter. So all the 3 broadcast receiver's onReceive()  method will be executed.

But since we are using ordered broadcast, first highest priority receiver will be executed. 

So Receiver2 of application1 will get executed first. (priority 3)
In the Receiver2, we are generating a random number between 0 to 100, and passing this as data to next receiver. (by using setResultCode() method).

Receiver3 of second application has next priority (priority 2), so that receiver will be called.
In the Receiver3 we will receive the data send by Receiver2 of first application. (by using getResultCode() method).

In the Receiver3 we will check if the data is even or odd. If it is even then we are stopping that broadcast in Receiver3 itself by using abortBroadCast() method. If not Receiver1 of first application will continue.

First Application's First Activity:

package com.techpalle.b15_rcvr_asg2;

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();
in.setAction("com.techpalle.action.ORDERED");
sendOrderedBroadcast(in, null);
}
});
}

}

First Application's Broadcast Receiver 1:

package com.techpalle.b15_rcvr_asg2;

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

public class Recvr1 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
int data = getResultCode();
Toast.makeText(context, "receiver 1..data.."+data, 0).show();
}
}

First Application's Broadcast Receiver 2:
package com.techpalle.b15_rcvr_asg2;

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

public class Recvr2 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "receiver 2..", 0).show();
int ran = (int) (Math.random()*100);
setResultCode(ran);
}
}

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" />

First application's Manifest file (to check the priorities of the receivers)
File name : AndroidManifest.xml

  
package="com.techpalle.b15_rcvr_asg2"
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_rcvr_asg2.MainActivity"
android:label="@string/app_name" >


















Second Application's First Activity:

package com.techpalle.b15_rcvr_asg2_app2;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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

Second Application's Broadcast Receiver 3:

package com.techpalle.b15_rcvr_asg2_app2;

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

public class Recvr3 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
int data = getResultCode();
if(data % 2 != 0){
abortBroadcast();
}
Toast.makeText(context, "receiver 3..data.."+data, 0).show();
}

}

Second application's Manifest file (to see the priority)
File name : AndroidManifest.xml

  
package="com.techpalle.b15_rcvr_asg2_app2"
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_rcvr_asg2_app2.MainActivity"
android:label="@string/app_name" >













Download complete code : Click to download

Android interview questions on Ordered broadcast receivers:
Difference between Normal broadcast, ordered broadcast, and sticky broadcast?

Tags: android, ordered, broadcast receivers, priority

Monday, March 23, 2015

Sticky broadcast receiver assignment: android tutorial

Program description:

This is an assignment program on using sticky broadcast receiver. This assignment has 2 applications. 

First application will be having one activity with one button. This activity also has a dynamic receiver which will be registered on clicking that button. 
Second application is also having one activity with one button. A sticky broadcast will be fired on clicking that button.

First user will open second application and clicks on the button to send a sticky broadcast to send a broadcast with action "com.techpalle.action.STICKY".

Then user will open first application's activity and click the button to register for receiver for an action "com.techpalle.action.STICKY". Once user clicks on the button to register dynamic receiver with an action which was previously broadcasted by second application, android will immediately call the onReceive() method of the current application's dynamic receiver. Reason for this is, sticky broadcasts will stick with android for future users.

Wanted to know more about what exactly is a sticky broadcast?

First Application's First activity:

package com.techpalle.b15_rcvr_asg1;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

private BroadcastReceiver br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Triggered..", 0).show();
}
};
private IntentFilter inf;
Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inf = new IntentFilter();
inf.addAction("com.techpalle.action.STICKY");

b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
registerReceiver(br, inf);
}
});
}

@Override
protected void onDestroy() {
unregisterReceiver(br);
super.onDestroy();
}

}

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" >

Manifest file
File name : AndroidManifest.xml

  
package="com.techpalle.b15_rcvr_asg1"
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_rcvr_asg1.MainActivity"
android:label="@string/app_name" >








Second Application's First activity:

package com.techpalle.b15_rcvr_asg1_app2;

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();
in.setAction("com.techpalle.action.STICKY");
sendStickyBroadcast(in);
}
});
}

}

xml file for first activity (second application)
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" />

Manifest file (for second application)
File name : AndroidManifest.xml

  
package="com.techpalle.b15_rcvr_asg1_app2"
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_rcvr_asg1_app2.MainActivity"
android:label="@string/app_name" >









Download complete code : Click to download

Android Interview questions related to Sticky broadcast receivers:
Difference between broadcast, sendorderedbroadcast, and sendstickybroadcast?
How to send battery low broadcast?
Difference between Intent, Sticky Intent, and Pending Intent?

Tags: android, sticky broadcast, receivers, sticky, dynamic receivers, sticky intent, permission, BROADCAST_STICKY.

Ordered broadcast receiver assignment: android tutorial

Program description:

This is an assignment program on using ordered broadcast receiver. This assignment has 2 applications. 


Assignment description: 
First application is having one activity, and 2 broadcast receivers (static).
Both the receivers have an intent filter with action "com.techpalle.action.ORDERED".
But Receiver1 has android:priority="1", and Receiver2 has android:priority="3".
In this first application first activity has a button to send ordered broadcast with action "com.techpalle.action.ORDERED". 

As you already know there are 3 types of broadcast receivers are there in android.

Note: More the priority number, higher the priority. Higher priority receivers will be executed first.

Second application has one activity, and 1 broadcast receiver (static).
This receiver has intent filter with same action "com.techpalle.action.ORDERED".
This Receiver3 has android:priority="2".

Assumption: It is assumed that the second application should be already in the phone or emulator. So make sure that you first run the second application and then first application.

Now once user runs the first application, and clicks the button on the first activity it will send an ordered broadcast. Now there are 3 receivers are matching with the intent action in their intent-filter. So all the 3 broadcast receiver's onReceive()  method will be executed.

But since we are using ordered broadcast, first highest priority receiver will be executed. 

So Receiver2 of application1 will get executed first. (priority 3)
In the Receiver2, we are generating a random number between 0 to 100, and passing this as data to next receiver. (by using setResultCode() method).

Receiver3 of second application has next priority (priority 2), so that receiver will be called.
In the Receiver3 we will receive the data send by Receiver2 of first application. (by using getResultCode() method).

In the Receiver3 we will check if the data is even or odd. If it is even then we are stopping that broadcast in Receiver3 itself by using abortBroadCast() method. If not Receiver1 of first application will continue.

First Application's First Activity:

package com.techpalle.b15_rcvr_asg2;

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();
in.setAction("com.techpalle.action.ORDERED");
sendOrderedBroadcast(in, null);
}
});
}

}

First Application's Broadcast Receiver 1:

package com.techpalle.b15_rcvr_asg2;

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

public class Recvr1 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
int data = getResultCode();
Toast.makeText(context, "receiver 1..data.."+data, 0).show();
}
}

First Application's Broadcast Receiver 2:
package com.techpalle.b15_rcvr_asg2;

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

public class Recvr2 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "receiver 2..", 0).show();
int ran = (int) (Math.random()*100);
setResultCode(ran);
}
}

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" />

First application's Manifest file (to check the priorities of the receivers)
File name : AndroidManifest.xml

  
package="com.techpalle.b15_rcvr_asg2"
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_rcvr_asg2.MainActivity"
android:label="@string/app_name" >


















Second Application's First Activity:

package com.techpalle.b15_rcvr_asg2_app2;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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

Second Application's Broadcast Receiver 3:

package com.techpalle.b15_rcvr_asg2_app2;

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

public class Recvr3 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
int data = getResultCode();
if(data % 2 != 0){
abortBroadcast();
}
Toast.makeText(context, "receiver 3..data.."+data, 0).show();
}

}

Second application's Manifest file (to see the priority)
File name : AndroidManifest.xml

  
package="com.techpalle.b15_rcvr_asg2_app2"
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_rcvr_asg2_app2.MainActivity"
android:label="@string/app_name" >













Download complete code : Click to download

Android interview questions on Ordered broadcast receivers:
Difference between Normal broadcast, ordered broadcast, and sticky broadcast?

Tags: android, ordered, broadcast receivers, priority

Broadcast receiver with implicit intent: Android tutorial

Program description:

This is one more program on how to create a broadcast receiver and how to send a broadcast message by using implicit intent.


This program has an activity, and 2 receivers. Activity has one button. On clicking the button an implicit intent will be used to send a broadcast with action com.techpalle.action.RECEIVER1.
This will trigger Receiver1. In Reciever1 we will send one more broadcast with an implicit intent with action com.techpalle.action.RECEIVER2. While sending this broadcast we will generate a random number between 0 to 100, and send that number as extra information in the intent.
This will trigger Receiver2, which displays random number passed from first broadcast receiver.

This program also has a demo of how to send a broadcast from one broadcast receiver to other broadcast receiver, by using implicit intent.

First Activity:

package com.techpalle.b15_asg_receivers;

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) {
// TODO Auto-generated method stub
Intent in = new Intent();
in.setAction("com.techpalle.action.RECEIVER1");
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 : Receiver1.java

package com.techpalle.b15_asg_receivers;

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

public class Receiver1 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "first app..receiver1", 0).show();
Intent in = new Intent();
in.setAction("com.techpalle.action.RECEIVER2");
int ran = (int) (Math.random()*100);
in.putExtra("ran", ran);
context.sendBroadcast(in);
}
}

Broadcast receiver
File name : Receiver2.java

package com.techpalle.b15_asg_receivers;

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

public class Receiver2 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int ran = intent.getExtras().getInt("ran");
Toast.makeText(context, "First app..recvr2..ran.."+ran, 0).show();
}
}

Manifest file
File name : AndroidManifest.xml


  
package="com.techpalle.b15_asg_receivers"
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_asg_receivers.MainActivity"
android:label="@string/app_name" >


















Download complete code : Click to download

Interview questions on broadcast receivers:
Broadcast receiver runs in which thread?
Is it possible to bind a service from receiver?

Tags: android, broadcast receiver, sendbroadcast, implicit intent

Older Posts Home