在Android中模拟广播

8

我需要知道如何模拟一个广播接收器(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>

你在清单文件中注册了哪些意图? - user936414
2个回答

8
请前往安装了android-sdk的文件夹,然后进入platform-tools文件夹。在那里会有一些可执行文件,其中之一是“adb”。
当进入该文件夹后,执行以下命令:
对于Mac/Linux系统: ./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
adb shell am 的官方文档:http://developer.android.com/tools/help/adb.html#am - Pang

0

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

它可能在功能上类似于@user1258903的答案,但应该能够发送远离的位置(例如:北极、妈妈的家、海底等),这样你就可以测试你可能拥有的计算和地图集成等。

如何在Android模拟器中仿真GPS位置?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接