如何从我的应用程序服务中退出另一个应用程序(不是我的应用程序),但不杀死它



我正在开发一个应用程序服务,该服务将显示在当前位于前台的另一个应用程序之上,如果用户按下后退按钮,它将破坏服务并退出应用程序,但不会杀死它。

我读过很多关于堆栈溢出的文章,告诉我使用 finish(( 或 finishAffinity((,但它只有在我自己的应用程序活动时才有效。

是否可以使用包名称退出应用?

FrameLayout interceptorLayout = new FrameLayout(this) {
  @Override
  public boolean dispatchKeyEvent(KeyEvent event) {
    // Only fire on the ACTION_DOWN event, or you'll get two events (one for _DOWN, one for _UP)
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
      // Check if the HOME button is pressed
      if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        Log.v(TAG, "BACK Button Pressed");
        onDestroy();
        // As we've taken action, we'll return true to prevent other apps from consuming the event as well
        return true;
      }
    } 
    // Otherwise don't intercept the event
    return super.dispatchKeyEvent(event);
  }
};

我不建议关闭其他应用程序。您可以使用其他应用的软件包名称退出在您的 AndroidManifest.xml 文件中声明该权限。在这里:

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

现在使用活动管理器类杀死其他应用程序,它将起作用。

ActivityManager am = (ActivityManager)getSystemService(Activity.ACTIVITY_SERVICE);
am.killBackgroundProcesses(packageName);

相关内容

最新更新