0

私はアプリで、子供が Android デバイスでゲームをプレイできる時間を監視しています。

以下を使用して、実行中のアプリのリストを取得する方法を知っています。

List<RunningTaskInfo> tasks = activityManager.getRunningTasks(1);//Integer.MAX_VALUE

今、私はこのアプリを閉じる方法、または単にバックグラウンドに置く方法が必要です。

私のコードを見てください:

private void verifyIfHasToCloseAnyApp() {
    ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = activityManager.getRunningTasks(1);//Integer.MAX_VALUE
    List<AppBlocked> appBlockeds = listAllAppBlocked();

        for (RunningTaskInfo runningTaskInfo : tasks) {

            String packageName = runningTaskInfo.topActivity.getPackageName();

            for(AppBlocked appBlocked : appBlockeds) {
                String packageBlocked = appBlocked.getAppPackage();
                if(packageName.equals(packageBlocked)) {
                    if(appBlocked.isTimeToClose())
            /*Here I have to close or put this app in background */
                }   
            }

        }
}

出来ますか?

ありがとう。

4

1 に答える 1

0

これは、標準の API では実行できません。編集または次のことができます: https://stackoverflow.com/a/7197265/1434792

でpsコマンドを実行できます。

Process process = Runtime.getRuntime().exec("ps");

そして、このコマンドの出力で強制終了するプロセスのpidを見つけ、このpidでkillコマンドを実行します。

    Process process = Runtime.getRuntime().exec("kill " + pid);

おそらく、これを行うにはルートアクセスが必要ですが、それについてはわかりません。これが必要な場合は、このプロセスを強制終了します

        Process process = Runtime.getRuntime().exec("su");

        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        InputStream is = process.getInputStream();
        os.writeBytes("kill " + pid+ "\n");
        int readed = 0;
        byte[] buff = new byte[4096];
                    os.writeBytes("exit\n");
        os.flush();
                process.destroy();
于 2014-01-02T07:21:46.400 に答える