【问题标题】:How to close application from another application in android如何从android中的另一个应用程序关闭应用程序
【发布时间】: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


    【解决方案1】:

    【讨论】:

    • 好的。但是是否可以从一个应用程序发送广播(意图)到另一个应用程序?
    • @user526206,是的,您接收广播的应用程序必须正在运行,但是从我所读到的内容来看,这是完全可能的。搜索 Stackoverflow 会返回几篇描述该过程的帖子。例如stackoverflow.com/questions/19304329/…
    • 对不起,它不起作用。两个应用程序都在运行,但我无法在 app2 中获得意图。我用 BroadcastReceiver 更新了我的问题...你能告诉我我做错了什么吗?
    【解决方案2】:

    这就是我解决这个问题的方法。对于发送意图:

            Intent intent = new Intent("com.hp.walk.CLOSE_APPLICATION");
        getApplication().sendBroadcast(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();
            }
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多