我正在从应用程序 A 向应用程序 B 发送广播,应用程序 B 有一个 BroadCastReceiver
来处理 Intent 。
现在我在App B的BroadcastReceiver
中的onReceive
中做一些操作,然后我想把结果返回给App A,这样我就可以回调,然后继续如果结果是积极的操作。
下面是一些代码。
在应用程序 B 中:
public class TripBackupReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
System.out.println("broadcast Received");
// send result back to Caller Activity
}
}
在应用 A 中:
Intent intent = new Intent("a_specified_action_string");
sendBroadcast(intent);
任何人都可以解决这个问题,请指导我一些建议。谢谢!!
最佳答案
为什么不只是(有点)从 App A 接收的 TripBackupReceiver
发送一个 Intent
?
类似于:
public class TripBackupReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("broadcast Received");
// send result back to Caller Activity
Intent replyIntent = new Intent("a_specified_action_string");
context.sendBroadcast(replyIntent);
}
}
您可以在 App A 中定义的 BroadcastReceiver
中接收此 Intent
。或者使用 Context.startActivity(Intent)
方法启动应用程序 A 中的 Activity 。
如果调用已在运行的 Activity ,您可能需要实现 onNewIntent()
。
请为此使用已签名的 Intent,以防止用户侵入您的 Intent API。
关于android - 广播接收器返回的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18867606/