简体   繁体   English

如何从Android中的另一个应用程序关闭应用程序

[英]How to close application from another application in android

I have two android applications. 我有两个android应用程序。 i start activity from App1 and when App2 activity finish i get results in App1 onActivityResult(). 我从App1开始活动,当App2活动完成时,我在App1 onActivityResult()中获得结果。 Now what i want that if i already open App2 activity and it is not finish yet and if i get some request in App1 to close the App2 activity then i close it without returning any result. 现在我想要的是,如果我已经打开App2活动并且尚未完成,并且如果我在App1中收到一些关闭App2活动的请求,则我将其关闭而不返回任何结果。

What i did, if i send intenet to "close_activity" signal to App2 activity from App1 like that: 我所做的是,如果我将互联网发送给“ close_activity”信号,例如从App1发送至App2活动:

 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);
    }

I guess only one way to send intenet to a running activity with this way. 我猜只有这样一种方法才能将Intenet发送到正在运行的活动。 so this will again start new activity. 因此这将再次开始新的活动。 App2 activity is not a singleTask because if i set it as a singleTask i unable to get results in App1 onActivityResult when App2 activity finish(). App2活动不是singleTask,因为如果我将其设置为singleTask,则当App2活动finish()时,我无法在App1 onActivityResult中获得结果。 Any hint how i can close the current running activity. 任何提示,我如何可以关闭当前正在运行的活动。

-----------------------------With BroadcastReceiver---------------------- -----------------------------使用BroadcastReceiver ------------------- ---

in App1 activity sending intent as broadcast: 在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);
    }

in App2 i have register BroadcastReceiver as: 在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");
    }
};

This is how i solve this issue. 这就是我解决这个问题的方式。 For sending intent: 发送意图:

        Intent intent = new Intent("com.hp.walk.CLOSE_APPLICATION");
    getApplication().sendBroadcast(intent);

in another application this is how i register receiver to get intent: 在另一个应用程序中,这就是我注册接收者以获取意图的方式:

@Override
public void onCreate(Bundle savedInstanceState) {

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("com.hp.walk.CLOSE_APPLICATION");
    registerReceiver(receiver, intentFilter);



 private BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if ("com.hp.walk.CLOSE_APPLICATION".equals(intent.getAction())) {
            Log.i(TAG, "received.. ");
            finish();
        }
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM