android:強制關閉其他應用


強制關閉其他應用,可以使用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種。。。。。。。。。。。。。

 

參考:http://blog.csdn.net/huxueyan521/article/details/8921976

         http://blog.csdn.net/mingli198611/article/details/7057615


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM