来自BroadcastReceiver beetwen Apps的回调?

时间:2014-03-10 13:31:13

标签: android android-intent callback broadcastreceiver

我想允许其他应用程序与我的集成,我正在编写一个虚拟的“消费者”应用程序,但如果一切顺利,我无法返回回复通知“消费者”应用程序。

所以我的DUMMY_APP有一个简单的布局,2个按钮成功调用,并且调用错误的EXTRA参数。 要使DUMMY_APP调用MAIN_APP,我使用sendBroadcast

// MainActivity class

private static final String REQUIRED_ACTION = "com.basetis.afr.intent.action.INIT_TEXT_FLOW";

onCreate....
    Button btnSuccess = (Button)findViewById(R.id.button_success_call);
    btnSuccess.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            i.setAction(REQUIRED_ACTION);
            i.putExtra(Intent.EXTRA_TEXT, textToBeRead);
            sendBroadcast(i);
        }
    });

所以MAIN_APP有相应的BroadcastReceiver接收正常。

// BlinkingReadReceiver class
private static final String CALLBACK_CALL_AFR_ACTION = "com.basetis.afr.intent.action.CALLBACK_CALL_AFR_ACTION";

@Override
public void onReceive(Context context, Intent intent) {
    Intent i = new Intent();
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    Log.d(TAG, "SUCCESS send callback");
    i.setAction(CALLBACK_CALL_AFR_ACTION);
    i.putExtra(CALL_AFR_SUCCESS_EXTRA, CALL_AFR_SUCCESS_EXTRA_DESC);
    i.setType("text/plain");

    context.sendBroadcast(i);
}

所以DUMMY_APP BroadcastReceiver永远不会收到任何东西:(

所以我配置了这样的清单:

DUMMY_APP

        <receiver android:name=".MainBroadcastReceiver" android:enabled="true">
            <intent-filter>
                <action android:name="com.basetis.afr.intent.action.CALLBACK_CALL_AFR_ACTION"></action>
            </intent-filter>
        </receiver>

MAIN_APP

    <receiver android:name=".BlinkingReadReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="com.basetis.afr.intent.action.INIT_TEXT_FLOW"></action>
        </intent-filter>
    </receiver>

有时候我收到这个错误(afrsender是de DUMMY_APP)但似乎有点随意...

Performing stop of activity that is not resumed: {com.basetis.afrsender.afrsender/com.basetis.afrsender.afrsender.MainActivity}
java.lang.RuntimeException: Performing stop of activity that is not resumed

有关如何实现这种双向App通信的任何建议吗?

非常感谢。

1 个答案:

答案 0 :(得分:0)

document

中所述
Starting from Android 3.1, the system's package manager keeps track of applications 
that are in a stopped state and provides a means of controlling their launch from 
background processes and other applications.

这意味着,在应用程序未由用户手动启动之前,您的应用程序将处于强制停止状态,并且不会接收任何广播。 这就是为什么你的虚拟应用程序没有接收和主应用程序发送的广播。 请查看here以获取更多参考资料