強制關閉其他應用,可以使用ActivityManager,首先需要獲取(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
然后可以調用其函數來進行關閉操作,目前來看有兩種方法:
1. void killBackgroundProcesses(String packageName):此方法在android官網的API中有介紹,在kill之后,被關閉的應用會被重新啟動。
Have the system immediately kill all background processes associated with the given package. This is the same as the kernel killing those processes to reclaim memory; the system will take care of restarting these processes in the future as needed.
使用:需要權限:<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
2. void forceStopPackage(String packageName):此方法目前沒有出現在android官網的API中,不知為何啊。。。。。。
使用:
需要權限:android:sharedUserId="android.uid.system"
<uses-permission android:name="android.permission.FOCE_STOP_PACKAGES" />
自己實現的代碼:
ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
activityManager.forceStopPackage("com.android.scan");
推薦使用第2種。。。。。。。。。。。。。