6

First, I found some similar questions on StackOverflow but none of the answers there solves my problem. I also googled it and read all the top 30 results.

So I am writing a Service that will check what is running in foreground every, say, 20 seconds. If the foreground application is on a blacklist, then I will popup a dialog Activity to say that "the current application needs to be stopped", then when user press OK of that dialog, that application will be stopped via the following method:

private void killApp(String packageName) {
    ActivityManager am = (ActivityManager)getApplicationContext().getSystemService(ACTIVITY_SERVICE);
    Log.d(TAG, "Trying to kill app " + packageName);
    am.killBackgroundProcesses(packageName);
}

However, the application is not stopped at all in my test. When the dialog is closed, the foreground app is still there, exactly the same state as it was before the dialog popped up. I believe I have the right API level as well as necessary permissions:

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="18" />

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

I am really stuck, any help will be greatly appreciated!

4

3 に答える 3

5

/ Android は、アプリやプロセスが画面に表示されている場合、つまりアクティブ ビューに表示されている場合、そのアプリやプロセスを強制終了しません。ホーム画面に切り替えてから、必要なアプリまたはプロセスを強制終了します/

    public void KillApplication(String KillPackage)
{
    ActivityManager am = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);    

    Intent startMain = new Intent(Intent.ACTION_MAIN); 
    startMain.addCategory(Intent.CATEGORY_HOME); 
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    this.startActivity(startMain);



    am.killBackgroundProcesses(KillPackage);
    Toast.makeText(getBaseContext(),"Process Killed : " + KillPackage  ,Toast.LENGTH_LONG).show();       
}
于 2014-04-08T14:32:53.353 に答える
2

次の操作を実行できます。

まず、 のタグandroid:sharedUserId="android.uid.system"内に追加します。マニフェスト ファイルは次のようになります。manifestAnfroidManifest.xml

<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication"
android:sharedUserId="android.uid.system">
...

次に、FORCE_STOP_PACKAGES許可を主張します。この手順により、マニフェスト ファイルは次のようになります。

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

第三に、停止したいパッケージについては、次のようにします。

final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
am.forceStopPackage(PACKAGE_NAME);

PACKAGE_NAMEパッケージ名に置き換えるだけです。

Eclipse を使用している場合は、 の lint エラーを無視してくださいPreferences > Android > Lint Error Checking

適切な権限を取得するには、apk をシステム ディレクトリに配置する必要があります。

于 2014-04-25T06:48:46.823 に答える
0

アプリにシステム権限、つまりシステムアプリを持たせるか、電話をルート化して、アプリがルートユーザーとして他のアプリを強制終了できるようにする必要があります。

于 2016-03-24T03:47:14.620 に答える