簡體   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