Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emulate a broadcast in Android

I need to know how to emulate a broadcastreceiver.

I have the following code, but I have no clue how to actually see when it is receiving a broadcast.

public class LocationBroadcastReceiver extends BroadcastReceiver 
{
@Override

public void onReceive(Context context, Intent intent) {


    Bundle b = intent.getExtras();
    Location loc = (Location)b.get(android.location.LocationManager.KEY_LOCATION_CHANGED);

    Toast.makeText(context, loc.toString(), Toast.LENGTH_SHORT).show(); 
    Log.d("com.dennis.test","LOCATION:  " + loc.toString());


}
}

In my manifest I have the following:

<receiver android:name="com.dennis.test.LocationBroadcastReceiver">
    <intent-filter>
        <action android:name="android.location.LocationManager.KEY_LOCATION_CHANGED" />
    </intent-filter>
</receiver>
like image 409
Dennis Avatar asked Mar 27 '12 08:03

Dennis


People also ask

How do you declare a broadcast receiver in manifest?

There are two ways to make a broadcast receiver known to the system: One is declare it in the manifest file with this element. The other is to create the receiver dynamically in code and register it with the Context. registerReceiver() method.

What is the role of the onReceive () method in the BroadcastReceiver?

onReceive. This method is called when the BroadcastReceiver is receiving an Intent broadcast. During this time you can use the other methods on BroadcastReceiver to view/modify the current result values.

What are the different types of broadcasts?

There are two types of broadcast receivers: Static receivers, which you register in the Android manifest file. Dynamic receivers, which you register using a context.


1 Answers

Go to the folder where your android-sdk is installed and then to platform-tools. There will be some executables. One of them is 'adb'.

When in the folder, execute

./adb shell am broadcast -a android.intent.action.KEY_LOCATION_CHANGED -c android.intent.category.HOME -n com.dennis.test/.LocationBroadcastReceiver

or on windows

adb shell am broadcast -a android.intent.action.KEY_LOCATION_CHANGED -c android.intent.category.HOME -n com.dennis.test/.LocationBroadcastReceiver

like image 101
user1258903 Avatar answered Sep 19 '22 13:09

user1258903