Mobile Security Guard-Cache cleaning and security guard cache cleaning
CleanCacheActivity. java
/*** Cache cleanup */public class CleanCacheActivity extends Activity {private PackageManager packageManager; private List <CacheInfo> cacheLists; private ListView list_view; @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); initUI ();} private void initUI () {setContentView (R. layout.Activity_clean_cache); List_view = (ListView) findViewById (R. id. list_view); // The garbage collection cacheLists = new ArrayList <CacheInfo> (); packageManager = getPackageManager (); new Thread () {public void run () {// List of all applications installed on the mobile phone <PackageInfo> installedPackages = packageManager. getInstalledPackages (0); // obtain the cache size of the application. for (PackageInfo packageInfo: installedPackages) {getCacheSize (packageInfo);} handler. sendEmptyMessage (0 );};}. start ();} private Handler handler = new Handler () {public void handleMessage (android. OS. message msg) {CacheAdapter adapter = new CacheAdapter (); list_view.setAdapter (adapter) ;}}; private class CacheAdapter extendsBaseAdapter{Private ViewHolder holder; @ Override public int getCount () {// TODO Auto-generated method stub return cacheLists. size () ;}@ Override public Object getItem (int arg0) {// TODO Auto-generated method stub return cacheLists. get (arg0) ;}@ Override public long getItemId (int arg0) {// TODO Auto-generated method stub return arg0 ;}@ Override public View getView (int position, View convertView, viewGroup parent) {// TODO Auto-generated method stub View = null; if (convertView = null) {view = view. inflate (CleanCacheActivity. this, R. layout.Item_clean_cache, Null); holder = new ViewHolder (); System. out. println ("111111111111"); holder. iv_icon = (ImageView) view. findViewById (R. id. iv_icon); holder. appname = (TextView) view. findViewById (R. id. TV _name); holder. cachesize = (TextView) view. findViewById (R. id. TV _cachesize); view. setTag (holder);} else {view = convertView; holder = (ViewHolder) view. getTag ();} System. out. println ("222222222222"); holder. iv_icon.setImageDrawable (cacheLists. get (position ). icon); holder. appname. setText (cacheLists. get (position ). appname); holder. cachesize. setText ("cache size:" + Formatter. formatFileSize (CleanCacheActivity. this, cacheLists. get (position ). cachesize); return view;} static class ViewHolder {ImageView iv_icon; TextView appname; TextView cachesize;} private voidGetCacheSize(PackageInfo packageInfo) {try {// Class <?> Clazz = getClassLoader (). loadClass ("packageManager"); // obtain the cache size through reflection. The third parameter isAidl object, the package we importedMethod method = PackageManager. class. getDeclaredMethod ("getPackageSizeInfo", String. class, IPackageStatsObserver. class);/** first parameter: who calls the current method and who calls the current method * second parameter: package name */method. invoke (packageManager, packageInfo. applicationInfo. packageName, new MyIPackageStatusObserver (packageInfo);} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace () ;}// aidl object private class MyIPackageStatusObserver extends IPackageStatsObserver. stub {private PackageInfo packageInfo; public MyIPackageAtatusObserver (PackageInfo packageInfo) {this. packageInfo = packageInfo; // TODO Auto-generated constructor stub} @ Override public void onGetStatsCompleted (PackageStats pStats, boolean succeeded) throws RemoteException {// TODO Auto-generated method stub // obtain the cache size of the current mobile phone application long cachesize = pStats. cacheSize; if (cachesize> 0) {// cache System. out. println ("Name of the current application:" + packageInfo. applicationInfo. loadLabel (packageManager) + "cache size:" + cachesize); CacheInfo cacheInfo = new CacheInfo (); Drawable icon = packageInfo. applicationInfo. loadIcon (packageManager); cacheInfo. icon = icon; String appname = packageInfo. applicationInfo. loadLabel (packageManager ). toString (); cacheInfo. appname = appname; cacheInfo. cachesize = cachesize; cacheLists. add (cacheInfo) ;}} static class CacheInfo {Drawable icon; long cachesize; String appname ;}// clear all public void cleanAll (View view) {// obtain all methods of the current application Method [] methods = packageManager. getClass (). getMethods (); for (Method method: methods) {// determine the current method name if (Method. getName (). equals ("freestorageandpolicy") {try {method. invoke (packageManager, Integer. MAX_VALUE, new MyIPackageDataObserver ();} catch (IllegalAccessException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IllegalArgumentException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (InvocationTargetException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} UIUtils. showToast (CleanCacheActivity. this, "Clear all");} private class MyIPackageDataObserver extends IPackageDataObserver. stub {@ Override public void onRemoveCompleted (String packageName, boolean succeeded) throws RemoteException {// TODO Auto-generated method stub }}}
We need to import the aidl file, such as import. Aidl is for inter-process communication. To learn aidl, we can refer to the http://www.open-open.com/lib/view/open1469494852171.html
Activity_clean_cache.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView style = "@ style/TitleStyle" android: text = "cache cleanup"/> <ListView android: id = "@ + id/list_view" android: layout_width = "match_parent" android: layout_height = "match_parent"Android: layout_weight = "111"> </ListView> <! --Render the listview.--> <Button android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "Clear all" android: onClick = "cleanAll" android: background = "@ drawable/btn_green_selector"/> </LinearLayout>
Item_clean_cache.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "horizontal"> <ImageView android: id = "@ + id/iv_icon" android: layout_width = "50dp" android: layout_height = "50dp" android: src = "@ drawable/ic_launcher"/> <LinearLayout android: layout_width = "174dp" android: layout_height = "wrap_content" android: layout_weight = "0.85" android: orientation = "vertical"> <TextView android: id = "@ + id/TV _name" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_marginTop = "5dp" android: text = "app name" android: textSize = "20dp"/> <TextView android: id = "@ + id/TV _cachesize" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_marginTop = "5dp" android: text = "cache size"/> </LinearLayout> <ImageView android: layout_width = "50dp" android: layout_height = "50dp" android: src = "@ drawable/list_button_clean_default"/> </LinearLayout>