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>

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


You should also be able to use MockLocation to generate from your executable or a JUnit. I have not used it, but that is its purpose.

It is probably functionally similar to @user1258903's answer, but should be able to send far away locations (ie, the North Pole, mom's house, bottom of the ocean, etc.) That way you can test out your computations and maps integration, etc. that you might have.

How to emulate GPS location in the Android Emulator?

链接地址: http://www.djcxy.com/p/90654.html

上一篇: 声明性地将宽度分配给一半可用的屏幕宽度

下一篇: 在Android中模拟广播