在Android中模拟广播

时间:2012-03-27 08:29:51

标签: android broadcastreceiver emulation

我需要知道如何模仿broadcastreceiver

我有以下代码,但我不知道如何实际看到它何时收到广播。

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());


}
}

在我的清单中,我有以下内容:

<receiver android:name="com.dennis.test.LocationBroadcastReceiver">
    <intent-filter>
        <action android:name="android.location.LocationManager.KEY_LOCATION_CHANGED" />
    </intent-filter>
</receiver>

2 个答案:

答案 0 :(得分:7)

转到安装android-sdk的文件夹,然后转到platform-tools。 会有一些可执行文件。其中一个是'adb'。

在文件夹中时,执行

  

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

或在Windows上

  

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

答案 1 :(得分:0)

您还应该能够使用MockLocation从您的可执行文件或JUnit生成。我没有使用它,但这是它的目的。

它可能在功能上与@ user1258903的答案相似,但应该能够发送远处的位置(即北极,妈妈的房子,海底等)。这样你可以测试你的计算和地图您可能拥有的整合等。

How to emulate GPS location in the Android Emulator?