(轉載) 清理緩存 IPackageStatsObserver

 

清理緩存 IPackageStatsObserver 

 分類:
 

目錄(?)[+]java

 


如今的位置:  首頁 > 綜合 > 正文
RSS
 
 

http://www.xuebuyuan.com/901153.html

Android中獲取應用程序(包)的大小—–PackageManager的使用(二)

2013年09月22日 ⁄ 綜合 ⁄ 共 20231字 ⁄ 字號    ⁄ 評論關閉

    經過第一部分<<Android中獲取應用程序(包)的信息-----PackageManager的使用(一)>>的介紹,對PackageManager以及android

AndroidManife.xml定義的節點信息類XXXInfo類都有了必定的認識。ios

          本部分的內容是如何獲取安裝包得大小,包括緩存大小(cachesize)、數據大小(datasize)、應用程序大小(codesize)。程序員

本部分的知識點涉及到AIDL、Java反射機制。理解起來也不是很難。緩存

   

      關於安裝包得大小信息封裝在PackageStats類中,該類很簡單,只有幾個字段:app

                PackageStats類:dom

                 經常使用字段:機器學習

                             public long cachesize           緩存大小異步

                             public long codesize             應用程序大小

                             public long datasize              數據大小

                             public String packageName  包名

 

         PS:應用程序的總大小 = cachesize  + codesize  + datasize

 

        也就是說只要得到了安裝包所對應的PackageStats對象,就能夠得到信息了。可是在AndroidSDK中並無顯示提供方法來

得到該對象,是否是很苦惱呢?可是,咱們能夠經過放射機制來調用系統中隱藏的函數(@hide)來得到每一個安裝包得信息。

具體方法以下:

 

        第一步、  經過放射機制調用getPackageSizeInfo()  方法原型爲:              

[java] 
view plain
copy
  1. /*@param packageName 應用程序包名 
  2.      *@param observer    當查詢包得信息大小操做完成後,將回調給IPackageStatsObserver類中的onGetStatsCompleted()方法, 
  3.      *      ,而且咱們須要的PackageStats對象也封裝在其參數裏. 
  4.      * @hide //隱藏函數的標記 
  5.      */  
  6.        public abstract void getPackageSizeInfo(String packageName,IPackageStatsObserver observer);{  
  7.           //   
  8.        }  

        內部調用流程以下,這個知識點較爲複雜,知道便可,

         getPackageSizeInfo方法內部調用getPackageSizeInfoLI(packageName, pStats)方法來完成包狀態獲取。

getPackageSizeInfoLI方法內部調用Installer.getSizeInfo(String pkgName, String apkPath,String fwdLockApkPath,   PackageStats

pStats),繼而將包狀態信息返回給參數pStats。getSizeInfo這個方法內部是以本機Socket方式鏈接到Server,

而後向server發送一個文本字符串命令,格式:getsize apkPath fwdLockApkPath 給server。Server將結果返回,並解析到pStats

中。掌握這個調用知識鏈便可。

 

 

     第二步、  因爲須要得到系統級的服務或類,咱們必須加入Android系統造成的AIDL文件,共兩個:

             IPackageStatsObserver.aidl 和 PackageStats.aidl文件。並將其放置在android.pm.content包路徑下。

   IPackageStatsObserver.aidl 文件

 

 

[java] 
view plain
copy
  1. package android.content.pm;  
  2.   
  3. import android.content.pm.PackageStats;  
  4. /** 
  5.  * API for package data change related callbacks from the Package Manager. 
  6.  * Some usage scenarios include deletion of cache directory, generate 
  7.  * statistics related to code, data, cache usage(TODO) 
  8.  * {@hide} 
  9.  */  
  10. oneway interface IPackageStatsObserver {  
  11.       
  12.     void onGetStatsCompleted(in PackageStats pStats, boolean succeeded);  
  13. }  

 

PackageStats.aidl文件

 

[java] 
view plain
copy
  1. package android.content.pm;  
  2.   
  3. parcelable PackageStats;  

 

       第三步、  建立一個類繼承至IPackageStatsObserver.Stub (樁,)它本質上實現了Binder機制。當咱們把該類的一個實例經過getPackageSizeInfo()調用時,並該函數繼而啓動了啓動中間流程去獲取相關包得信息大小,當掃描完成後,最後將查詢信息回調至該類的onGetStatsCompleted(in PackageStats pStats, boolean succeeded)方法,信息大小封裝在此實例上。例如:

 

[java] 
view plain
copy
  1. //aidl文件造成的Bindler機制服務類   
  2.    public class PkgSizeObserver extends IPackageStatsObserver.Stub{  
  3.        /*** 回調函數, 
  4.         * @param pStatus ,返回數據封裝在PackageStats對象中 
  5.         * @param succeeded  表明回調成功 
  6.         */   
  7. @Override  
  8. public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)  
  9.         throws RemoteException {  
  10.    // TODO Auto-generated method stub 
      
  11.    cachesize = pStats.cacheSize  ; //緩存大小
      
  12.           datasize = pStats.codeSize  ;  //數據大小 
      
  13.           codesize =    pStats.codeSize  ;  //應用程序大小
      
  14.      }  
  15.   }  

       

       第四步、  最後咱們能夠獲取 pStats的屬性,得到它們的屬性值,經過調用系統函數Formatter.formateFileSize(long size)轉換

爲對應的以kb/mb爲計量單位的字符串。

 

     很重要的一點:爲了可以經過反射獲取應用程序大小,咱們必須加入如下權限,不然,會出現警告而且得不到實際值。

       

[java] 
view plain
copy
  1. <uses-permission android:name="android.permission.GET_PACKAGE_SIZE"></uses-permission>  

 

     流程圖以下:

            

 

Demo說明

              在第一部分應用得基礎上,咱們添加了一個新功能,點擊任何一個應用後後,彈出顯示該應用的包信息大小的對話框。

        截圖以下:

                 工程圖:                                                                                  程序效果圖:

                          

 

一、dialg_app_size.xml 文件

[html] 
view plain
copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content">  
  5.     <LinearLayout android:layout_width="wrap_content"  
  6.         android:layout_height="wrap_content" android:orientation="horizontal">  
  7.         <TextView android:layout_width="100dip"  
  8.             android:layout_height="wrap_content" android:text="緩存大小:"></TextView>  
  9.         <TextView android:layout_width="100dip" android:id="@+id/tvcachesize"  
  10.             android:layout_height="wrap_content"></TextView>  
  11.     </LinearLayout>  
  12.     <LinearLayout android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content" android:orientation="horizontal">  
  14.         <TextView android:layout_width="100dip"  
  15.             android:layout_height="wrap_content" android:text="數據大小:"></TextView>  
  16.         <TextView android:layout_width="100dip" android:id="@+id/tvdatasize"  
  17.             android:layout_height="wrap_content"></TextView>  
  18.     </LinearLayout>  
  19.     <LinearLayout android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content" android:orientation="horizontal">  
  21.         <TextView android:layout_width="100dip"  
  22.             android:layout_height="wrap_content" android:text="應用程序大小:"></TextView>  
  23.         <TextView android:layout_width="100dip" android:id="@+id/tvcodesize"  
  24.             android:layout_height="wrap_content"></TextView>  
  25.     </LinearLayout>  
  26.     <LinearLayout android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content" android:orientation="horizontal">  
  28.         <TextView android:layout_width="100dip"  
  29.             android:layout_height="wrap_content" android:text="總大小:"></TextView>  
  30.         <TextView android:layout_width="100dip" android:id="@+id/tvtotalsize"  
  31.             android:layout_height="wrap_content"></TextView>  
  32.     </LinearLayout>  
  33. </LinearLayout>  

 

  二、另外的資源文件或自定義適配器複用了第一部分,請知悉。

  三、添加AIDL文件,如上。

  四、主文件MainActivity.java以下:

 

[java] 
view plain
copy
  1. package com.qin.appsize;  
  2.   
  3.   
  4. import java.lang.reflect.Method;  
  5. import java.util.ArrayList;  
  6. import java.util.Collections;  
  7. import java.util.List;  
  8.   
  9. import com.qin.appsize.AppInfo;  
  10.   
  11. import android.app.Activity;  
  12. import android.app.AlertDialog;  
  13. import android.content.ComponentName;  
  14. import android.content.Context;  
  15. import android.content.DialogInterface;  
  16. import android.content.Intent;  
  17. import android.content.pm.IPackageStatsObserver;  
  18. import android.content.pm.PackageManager;  
  19. import android.content.pm.PackageStats;  
  20. import android.content.pm.ResolveInfo;  
  21. import android.graphics.drawable.Drawable;  
  22. import android.os.Bundle;  
  23. import android.os.RemoteException;  
  24. import android.text.format.Formatter;  
  25. import android.util.Log;  
  26. import android.view.LayoutInflater;  
  27. import android.view.View;  
  28. import android.widget.AdapterView;  
  29. import android.widget.ListView;  
  30. import android.widget.TextView;  
  31. import android.widget.AdapterView.OnItemClickListener;  
  32.   
  33. public class MainActivity extends Activity implements OnItemClickListener{  
  34.     private static String TAG = "APP_SIZE";  
  35.   
  36.     private ListView listview = null;  
  37.     private List<AppInfo> mlistAppInfo = null;  
  38.     LayoutInflater infater = null ;   
  39.     //全局變量,保存當前查詢包得信息   
  40.     private long cachesize ; //緩存大小
      
  41.     private long datasize  ;  //數據大小 
      
  42.     private long codesize  ;  //應用程序大小
      
  43.     private long totalsize ; //總大小
      
  44.     @Override  
  45.     public void onCreate(Bundle savedInstanceState) {  
  46.         super.onCreate(savedInstanceState);  
  47.         setContentView(R.layout.browse_app_list);  
  48.         listview = (ListView) findViewById(R.id.listviewApp);  
  49.         mlistAppInfo = new ArrayList<AppInfo>();  
  50.         queryAppInfo(); // 查詢全部應用程序信息 
      
  51.         BrowseApplicationInfoAdapter browseAppAdapter = new BrowseApplicationInfoAdapter(  
  52.                 this, mlistAppInfo);  
  53.         listview.setAdapter(browseAppAdapter);  
  54.         listview.setOnItemClickListener(this);  
  55.     }  
  56.      // 點擊彈出對話框,顯示該包得大小   
  57.     public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {  
  58.         //更新顯示當前包得大小信息   
  59.         queryPacakgeSize(mlistAppInfo.get(position).getPkgName());   
  60.           
  61.         infater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  62.         View dialog = infater.inflate(R.layout.dialog_app_size, null) ;  
  63.         TextView tvcachesize =(TextView) dialog.findViewById(R.id.tvcachesize) ; //緩存大小
      
  64.         TextView tvdatasize = (TextView) dialog.findViewById(R.id.tvdatasize)  ; //數據大小
      
  65.         TextView tvcodesize = (TextView) dialog.findViewById(R.id.tvcodesize) ; // 應用程序大小
      
  66.         TextView tvtotalsize = (TextView) dialog.findViewById(R.id.tvtotalsize) ; //總大小
      
  67.         //類型轉換並賦值   
  68.         tvcachesize.setText(formateFileSize(cachesize));  
  69.         tvdatasize.setText(formateFileSize(datasize)) ;  
  70.         tvcodesize.setText(formateFileSize(codesize)) ;  
  71.         tvtotalsize.setText(formateFileSize(totalsize)) ;  
  72.         //顯示自定義對話框   
  73.         AlertDialog.Builder builder =new AlertDialog.Builder(MainActivity.this) ;  
  74.         builder.setView(dialog) ;  
  75.         builder.setTitle(mlistAppInfo.get(position).getAppLabel()+"的大小信息爲:") ;  
  76.         builder.setPositiveButton("肯定", new DialogInterface.OnClickListener() {  
  77.   
  78.             @Override  
  79.             public void onClick(DialogInterface dialog, int which) {  
  80.                 // TODO Auto-generated method stub
      
  81.                 dialog.cancel() ;  // 取消顯示對話框
      
  82.             }  
  83.               
  84.         });  
  85.         builder.create().show() ;  
  86.     }  
  87.     public void  queryPacakgeSize(String pkgName) throws Exception{  
  88.         if ( pkgName != null){  
  89.             //使用放射機制獲得PackageManager類的隱藏函數getPackageSizeInfo
      
  90.             PackageManager pm = getPackageManager();  //獲得pm對象
      
  91.             try {  
  92.                 //經過反射機制得到該隱藏函數   
  93.                 Method getPackageSizeInfo = pm.getClass().getDeclaredMethod("getPackageSizeInfo", String.class,IPackageStatsObserver.class);  
  94.                 //調用該函數,而且給其分配參數 ,待調用流程完成後會回調PkgSizeObserver類的函數
      
  95.                 getPackageSizeInfo.invoke(pm, pkgName,new PkgSizeObserver());  
  96.             }   
  97.             catch(Exception ex){  
  98.                 Log.e(TAG, "NoSuchMethodException") ;  
  99.                 ex.printStackTrace() ;  
  100.                 throw ex ;  // 拋出異常
      
  101.             }   
  102.         }  
  103.     }  
  104.      
  105.     //aidl文件造成的Bindler機制服務類 
      
  106.     public class PkgSizeObserver extends IPackageStatsObserver.Stub{  
  107.         /*** 回調函數, 
  108.          * @param pStatus ,返回數據封裝在PackageStats對象中 
  109.          * @param succeeded  表明回調成功 
  110.          */   
  111.         @Override  
  112.         public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)  
  113.                 throws RemoteException {  
  114.             // TODO Auto-generated method stub
      
  115.            cachesize = pStats.cacheSize  ; //緩存大小
      
  116.             datasize = pStats.dataSize  ;  //數據大小 
      
  117.             codesize = pStats.codeSize  ;  //應用程序大小
      
  118.             totalsize = cachesize + datasize + codesize ;  
  119.             Log.i(TAG, "cachesize--->"+cachesize+" datasize---->"+datasize+ " codeSize---->"+codesize)  ;  
  120.         }  
  121.     }  
  122.     //系統函數,字符串轉換 long -String (kb) 
      
  123.     private String formateFileSize(long size){  
  124.         return Formatter.formatFileSize(MainActivity.this, size);   
  125.     }  
  126.    // 得到全部啓動Activity的信息,相似於Launch界面 
      
  127.     public void queryAppInfo() {  
  128.         PackageManager pm = this.getPackageManager(); // 得到PackageManager對象
      
  129.         Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);  
  130.         mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
  131.         // 經過查詢,得到全部ResolveInfo對象.
      
  132.         List<ResolveInfo> resolveInfos = pm.queryIntentActivities(mainIntent, 0);  
  133.         // 調用系統排序 , 根據name排序 
      
  134.         // 該排序很重要,不然只能顯示系統應用,而不能列出第三方應用程序 
      
  135.         Collections.sort(resolveInfos,new ResolveInfo.DisplayNameComparator(pm));  
  136.         if (mlistAppInfo != null) {  
  137.             mlistAppInfo.clear();  
  138.             for (ResolveInfo reInfo : resolveInfos) {  
  139.                 String activityName = reInfo.activityInfo.name; // 得到該應用程序的啓動Activity的name
      
  140.                 String pkgName = reInfo.activityInfo.packageName; // 得到應用程序的包名
      
  141.                 String appLabel = (String) reInfo.loadLabel(pm); // 得到應用程序的Label
      
  142.                 Drawable icon = reInfo.loadIcon(pm); // 得到應用程序圖標
      
  143.                 // 爲應用程序的啓動Activity 準備Intent
      
  144.                 Intent launchIntent = new Intent();  
  145.                 launchIntent.setComponent(new ComponentName(pkgName,activityName));  
  146.                 // 建立一個AppInfo對象,並賦值 
      
  147.                 AppInfo appInfo = new AppInfo();  
  148.                 appInfo.setAppLabel(appLabel);  
  149.                 appInfo.setPkgName(pkgName);  
  150.                 appInfo.setAppIcon(icon);  
  151.                 appInfo.setIntent(launchIntent);  
  152.                 mlistAppInfo.add(appInfo); // 添加至列表中
      
  153.             }  
  154.         }  
  155.     }  
  156. }  

      獲取應用程序信息大小就是這麼來的,整個過程相對而言仍是挺簡單的,比較難理解的是AIDL文件的使用和回調函數的處理。

仔細研究後,纔有所理解。

 

 

package com.itheima.mobilesafe74.activity;


import java.lang.reflect.Method;
import java.util.List;
import java.util.Random;


import com.itheima.mobilesafe74.R;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.IPackageDataObserver;
import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.PackageStats;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;


public class CacheClearActivity extends Activity {
protected static final int UPDATE_CACHE_APP = 100;
protected static final int CHECK_CACHE_APP = 101;
protected static final int CHECK_FINISH = 102;
protected static final int CLEAR_CACHE = 103;
protected static final String tag = "CacheClearActivity";

private Button bt_clear;
private ProgressBar pb_bar;
private TextView tv_name;
private LinearLayout ll_add_text;
private PackageManager mPm;
private int mIndex = 0;

private Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_CACHE_APP:
//8.在線性佈局中添加有緩存應用條目
View view = View.inflate(getApplicationContext(), R.layout.linearlayout_cache_item, null);

ImageView iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
TextView tv_item_name = (TextView) view.findViewById(R.id.tv_name);
TextView tv_memory_info = (TextView)view.findViewById(R.id.tv_memory_info);
ImageView iv_delete = (ImageView) view.findViewById(R.id.iv_delete);

final CacheInfo cacheInfo = (CacheInfo) msg.obj;
iv_icon.setBackgroundDrawable(cacheInfo.icon);
tv_item_name.setText(cacheInfo.name);
tv_memory_info.setText(Formatter.formatFileSize(getApplicationContext(), cacheInfo.cacheSize));

ll_add_text.addView(view, 0);

iv_delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//清除單個選中應用的緩存內容(PackageMananger)

/* 如下代碼若是要執行成功則須要系統應用才能夠去使用的權限
* android.permission.DELETE_CACHE_FILES
* try {
Class<?> clazz = Class.forName("android.content.pm.PackageManager");
//2.獲取調用方法對象
Method method = clazz.getMethod("deleteApplicationCacheFiles", String.class,IPackageDataObserver.class);
//3.獲取對象調用方法
method.invoke(mPm, cacheInfo.packagename,new IPackageDataObserver.Stub() {
@Override
public void onRemoveCompleted(String packageName, boolean succeeded)
throws RemoteException {
//刪除此應用緩存後,調用的方法,子線程中
Log.i(tag, "onRemoveCompleted.....");
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
//源碼開發課程(源碼(handler機制,AsyncTask(異步請求,手機啓動流程)源碼))
//經過查看系統日誌,獲取開啓清理緩存activity中action和data
Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setData(Uri.parse("package:"+cacheInfo.packagename));
startActivity(intent);
}
});
break;
case CHECK_CACHE_APP:
tv_name.setText((String)msg.obj);
break;
case CHECK_FINISH:
tv_name.setText("掃描完成");
break;
case CLEAR_CACHE:
//從線性佈局中移除全部的條目
ll_add_text.removeAllViews();
break;
}
};
};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cache_clear);
initUI();
initData();
}


/**
* 遍歷手機全部的應用,獲取有緩存的應用,用做顯示
*/
private void initData() {
new Thread(){
public void run() {
//1.獲取包管理者對象

mPm = getPackageManager();

//2.獲取安裝在手機上的全部的應用
List<PackageInfo> installedPackages = mPm.getInstalledPackages(0);
//3.給進度條設置最大值(手機中全部應用的總數)
pb_bar.setMax(installedPackages.size());
//4.遍歷每個應用,獲取有緩存的應用信息(應用名稱,圖標,緩存大小,包名)
for (PackageInfo packageInfo : installedPackages) {
//包名做爲獲取緩存信息的條件
String packageName = packageInfo.packageName;
getPackageCache(packageName);

try {
Thread.sleep(100+new Random().nextInt(50));
} catch (InterruptedException e) {
e.printStackTrace();
}
mIndex++;
pb_bar.setProgress(mIndex);

//每循環一次就將檢測應用的名稱發送給主線程顯示
Message msg = Message.obtain();
msg.what = CHECK_CACHE_APP;
String name = null;
try {
name = mPm.getApplicationInfo(packageName, 0).loadLabel(mPm).toString();
} catch (NameNotFoundException e) {
e.printStackTrace();
}
msg.obj = name;
mHandler.sendMessage(msg);
}
Message msg = Message.obtain();
msg.what = CHECK_FINISH;
mHandler.sendMessage(msg);
};
}.start();
}

class CacheInfo{
public String name;
public Drawable icon;
public String packagename;
public long cacheSize;
}


/**經過包名獲取此包名指向應用的緩存信息
* @param packageName 應用包名
*/
protected void getPackageCache(String packageName) {
IPackageStatsObserver.Stub mStatsObserver = new IPackageStatsObserver.Stub() {


public void onGetStatsCompleted(PackageStats stats,
boolean succeeded) {
//子線程中方法,用到消息機制

//4.獲取指定包名的緩存大小
long cacheSize = stats.cacheSize;
//5.判斷緩存大小是否大於0
if(cacheSize>0){
//6.告知主線程更新UI
Message msg = Message.obtain();
msg.what = UPDATE_CACHE_APP;
CacheInfo cacheInfo = null;
try {
//7.維護有緩存應用的javabean
cacheInfo = new CacheInfo();
cacheInfo.cacheSize = cacheSize;
cacheInfo.packagename = stats.packageName;
cacheInfo.name = mPm.getApplicationInfo(stats.packageName, 0).loadLabel(mPm).toString();
cacheInfo.icon = mPm.getApplicationInfo(stats.packageName, 0).loadIcon(mPm);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
msg.obj = cacheInfo;
mHandler.sendMessage(msg);
}
}
};
//1.獲取指定類的字節碼文件
try {
Class<?> clazz = Class.forName("android.content.pm.PackageManager");
//2.獲取調用方法對象
Method method = clazz.getMethod("getPackageSizeInfo", String.class,IPackageStatsObserver.class);
//3.獲取對象調用方法
method.invoke(mPm, packageName,mStatsObserver);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


private void initUI() {
bt_clear = (Button) findViewById(R.id.bt_clear);
pb_bar = (ProgressBar) findViewById(R.id.pb_bar);
tv_name = (TextView) findViewById(R.id.tv_name);
ll_add_text = (LinearLayout) findViewById(R.id.ll_add_text);

bt_clear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//1.獲取指定類的字節碼文件
try {
Class<?> clazz = Class.forName("android.content.pm.PackageManager");
//2.獲取調用方法對象
Method method = clazz.getMethod("freeStorageAndNotify", long.class,IPackageDataObserver.class);
//3.獲取對象調用方法
method.invoke(mPm, Long.MAX_VALUE,new IPackageDataObserver.Stub() {
@Override
public void onRemoveCompleted(String packageName, boolean succeeded)
throws RemoteException {
//清除緩存完成後調用的方法(考慮權限)
Message msg = Message.obtain();
msg.what = CLEAR_CACHE;
mHandler.sendMessage(msg);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
 
1
0
 
 
 

 

 
查看評論

  暫無評論

 
 
* 以上用戶言論只表明其我的觀點,不表明CSDN網站的觀點或立場
相關文章
相關標籤/搜索