在Android中模拟广播

Den*_*nis 6 android emulation broadcastreceiver

我需要知道如何模仿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());


}
}
Run Code Online (Sandbox Code Playgroud)

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

<receiver android:name="com.dennis.test.LocationBroadcastReceiver">
    <intent-filter>
        <action android:name="android.location.LocationManager.KEY_LOCATION_CHANGED" />
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

小智 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

或在窗户上

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