App Signals (Broadcast Receiver)

kingfishblacksmithΚινητά – Ασύρματες Τεχνολογίες

14 Δεκ 2013 (πριν από 3 χρόνια και 8 μήνες)

196 εμφανίσεις

Page
1

of
7


BroadCast Receiver in
Android




by Anshu Agrawal




Page
2

of
7


A
broadcast receiver

is a component that responds to system
-
wide broadcast announcements. Many
broadcasts originate from the system

for example, a broadcast announcing that the screen has turned off,
the battery is low, or a picture was captured. Applications can also initiat
e broadcasts

for example, to let
other applications know that some data has been downloaded to the device and is available for them to
use. Although broadcast receivers don't display a user interface, they may
create

a

status

bar

notification

to alert the user when a broadcast event occurs. More commonly, though, a broadcast receiver is just a
"gateway" to other components and is intended to do a very minimal amount of work. For instance, it
might initiate a servic
e to perform some work based on the event.

Steps:

1.

Goto on the left side of the eclipse on File
New
other
Android Project.

2.

New Android Project window will get open.

3.

Write and_Broadcast in the Project name.

4.

Select Android1.5 under Build target.




5.

Fill
application name as and_Broadcast.

6.

Fill package name as ans.and_Broadcast

7.

Create Activity and_Broadcast.

8.

Click on Finish Button.


Page
3

of
7





9.

Now and_Broadcast project will be created under Project Explorer in Eclipse.

10.

Double click on and_Broadcast, then open src
ans.and_Broadcast.

11.

In the folder ans.and_Broadcast and_Broadcast.java will be created.

12.

Now Right click on ans.and_Broadcast,select New
class.




Page
4

of
7


13.

A new Java Class window will open.

14.

Fill Name as Broadcast_exp.



15.

Click on finish Button.

16.

Now the Class
Broadcast_exp.java will be created.



17.

Copy the below code in Broadcast_exp.java .


Page
5

of
7


package

ans.and_Broadcast;


import

android.content.BroadcastReceiver;

import

android.content.Context;

import

android.content.Intent;

import

android.widget.Toast;


public

class

Broadcast_exp
extends

BroadcastReceiver {



private

static

final

int

LENGTH_LONG

= 1000;



public

void

onReceive(Context arg0, Intent intent) {



if

(intent.getAction().equalsIgnoreCase(
"anshu"
)) {




longtoast(arg0,
"ANSHU "

+ intent.getAction(),
LE
NGTH_LONG
);




}



if

(intent.getAction().equalsIgnoreCase(
"Hello"
)) {




longtoast(arg0,
"Hello Hello "

+ intent.getAction(),
LENGTH_LONG
);




}



if

(intent.getAction().equalsIgnoreCase(





"android.intent.action.BOOT_COMPLETED"
)) {




longtoast(arg0,
"BOOT COMPLETED"
,
LENGTH_LONG
);



}



}



private

void

longtoast(Context context, String st,
int

dir) {



Toast.
makeText
(context, st, dir).show();


}

}


18.

Copy the below code in the file and_Broadcast.java.

package

ans.and_Broadcast;


import

android.app.Activity;

import

android.content.Intent;

import

android.os.Bundle;


public

class

and_Broadcast
extends

Activity {


/** Called when the activity is first created. */


@Override


public

void

onCreate(Bundle savedInstanceState) {



super
.onCreate(savedInstanceState);



setContentView(R.layout.
main
);




Intent intent =
new

Intent(
this
, Broadcast_exp.
class
);



intent.setAction(
"anshu"
);



Bundle extras =
new

Bundle();



extras.putString(
"anshu"
,
"Hello haal kaisa hai janaam ka "
);



intent.putExtra(
"bundle1"
, extras);



sendBroadcast(intent);




intent.setAction(
"Hello"
);



sendBroadcast(intent);

Page
6

of
7



}

}


19.

Copy the below code in AndroidManifest.xml file.


<?
xml

version
=
"1.0"

encoding
=
"utf
-
8"
?>

<
manifest

xmlns:android
=
"http:/
/schemas.android.com/apk/res/android"


package
=
"ans.and_Broadcast"


android:versionCode
=
"1"


android:versionName
=
"1.0"
>




<
application

android:icon
=
"@drawable/icon"

android:label
=
"@string/app_name"
>


<
activity

android:name
=
".and_B
roadcast"


android:label
=
"@string/app_name"
>




<
intent
-
filter
>


<
action

android:name
=
"android.intent.action.MAIN"

/>


<
category

android:name
=
"android.intent.category.LAUNCHER"

/>



</
intent
-
filter
>




</
activity
>


<
receiver

android:name
=
".Broadcast_exp"
>


<
intent
-
filter
>

<
action

android:name
=
"android.intent.action.BOOT_COMPLETED"
/>


</
intent
-
filter
>


</
receiver
>



</
applicati
on
>

</
manifest
>


20.

Now click on Run
Run Configurations.


21.

On the Run Configurations Window click on Android application
New Configuration.

22.

Under Android Tab , click on the browse button and on Project Selection window ,select
and_Broadcast.


Page
7

of
7



1.

Click on OK
and then ON Run Button of the Run Configurations Window.

2.

When the application is loaded on the Emulator, then below is the result
-

Toast Message saying
ANSHU anshu.