基于shell am命令创建广播意图

时间:2017-11-09 08:41:14

标签: android shell android-intent broadcastreceiver android-broadcast

我正在使用OWASP Goat Droid项目。该应用程序具有以下广播过滤器意图过滤器:

[...]
        <receiver android:label="Send SMS" android:name=".broadcastreceivers.SendSMSNowReceiver">
            <intent-filter>
                <action android:name="org.owasp.goatdroid.fourgoats.SOCIAL_SMS"/>
            </intent-filter> &gt;
        </receiver>
[...]

我可以使用设备上的交互式shell成功发送广播到这个包/ class / intent-filter(仍然不确定这里的正确术语),使用以下命令:

adb shell
am broadcast –a MyBroadcastAction –n org.owasp.goatdroid.fourgoats/.broadcastreceivers.SendSMSNowReceiver –-es phoneNumber 1234-–es message “Test”

我正在尝试编写应用,其中应用完全相同的操作。按下按钮后,在新活动中执行以下代码。

    Intent intent = new Intent();
    intent.setAction("MyBroadcastAction ");

    ComponentName componentName = new ComponentName("org.owasp.goatdroid.fourgoats", ".broadcastreceivers.SendSMSNowReceiver");
    intent.setComponent(componentName);

    Bundle extras = new Bundle();
    extras.putString("phoneNumber","1234");
    extras.putString("message","APP_Test");
    intent.putExtras(extras);

    sendBroadcast(intent);

Goat Droid没有收到申请,经过调查,似乎没有发送广播。

adb shell
dumpsys activity broadcasts | grep -B 10 -A 10 goat

这只显示我从交互式shell发送的广播。

如何以应用形式正确发送广播,如交互式外壳所示?

编辑: Goat Droid应用程序安装在AVD(API级别24)上,启动上述意图的应用程序直接通过AVD上的Android SDK启动。

编辑:已解决

但我不太确定如何/为什么。阅读JMeter web test plan后来到解决方案。以下代码适用于我:

    Intent intent = new Intent("org.owasp.goatdroid.fourgoats.SOCIAL_SMS");
    Bundle extras = new Bundle();
    extras.putString("phoneNumber","1234");
    extras.putString("message","test");
    intent.putExtras(extras);
    sendBroadcast(intent);

我必须使用相应的操作创建intent,而交互式shell am命令不需要该操作。

0 个答案:

没有答案