繁体   English   中英

我想通过在当前应用程序中按一个按钮来终止/关闭特定应用程序。 我怎样才能做到这一点?

[英]I want to kill/ close a particular app by pressing a button in my current app. How can I do this?

这就是我想要的:我的应用程序有一个用于关闭另一个独立应用程序的按钮-Google地图。 我想知道该过程是否已经开始,如果已经开始,那么我想通过按该按钮将其关闭。

您可以使用BroadcastReceiver类。 尝试这种方式。

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (isAppForground(context)) {
            // App is in Foreground
        } else {
            // App is in Background
        }
    }

    public boolean isAppForground(Context mContext) {

        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(mContext.getPackageName())) {
                return false;
            }
        }

        return true;
    }

暂无
暂无

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

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