【发布时间】:2016-02-15 15:55:28
【问题描述】:
我有两个安卓应用程序。我从 App1 开始活动,当 App2 活动完成时,我在 App1 onActivityResult() 中得到结果。 现在我想要的是,如果我已经打开 App2 活动并且它还没有完成,并且如果我在 App1 中收到一些关闭 App2 活动的请求,那么我将其关闭而不返回任何结果。
我做了什么,如果我像这样从 App1 向 App2 活动发送“close_activity”信号:
Log.i(TAG, "stoping app..");
try {
Intent intent = mContext.getPackageManager().getLaunchIntentForPackage("com.company.communicationApp");
intent.setAction("CANCEL_CALL");
context.startActivity(intent);
} catch (Exception e) {
Log.e(TAG, "Unable stop app", e);
}
我猜只有一种方法可以通过这种方式将互联网发送到正在运行的活动。所以这将再次开始新的活动。 App2 活动不是单任务,因为如果我将其设置为单任务,当 App2 活动完成()时,我无法在 App1 onActivityResult 中获得结果。 任何提示我如何关闭当前正在运行的活动。
------------------带有广播接收器---------------- ------
在 App1 活动中以广播形式发送意图:
try {
final Intent intent = new Intent();
intent.setAction("CANCEL_CALL");
intent.setComponent(new ComponentName("com.hos.hp.talk", "com.hos.hp.talk.MainActivity"));
mContext.sendBroadcast(intent);
} catch (Exception e) {
Log.e(TAG, "Unable stop communication app", e);
}
在 App2 中,我将 BroadcastReceiver 注册为:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("CANCEL_CALL");
registerReceiver(receiver, intentFilter);
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("ddd");
}
};
【问题讨论】:
标签: android android-intent android-activity