Android_反射技术_调用类中私有的方法

为了使用私有方法:getPackageSizeInfo

Method method = PackageManager.class.getMethod("getPackageSizeInfo", new Class[]{String.class,

public class DemoActivity extends Activity {
    private PackageManager pm;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        pm = getPackageManager();

         try {
            Method method = PackageManager.class.getMethod("getPackageSizeInfo", new Class[]{String.class,IPackageStatsObserver.class});
            
            method.invoke(pm, new Object[]{"cn.itcast.mobilesafe",new IPackageStatsObserver.Stub() {
                
                public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
                        throws RemoteException {
                    System.out.println("----------------");
                    long cachesize = pStats.cacheSize;
                    long codesize = pStats.codeSize;
                    long datasize = pStats.dataSize;
                    System.out.println("datasize"+datasize);
                    System.out.println("codesize"+codesize);
                    System.out.println("cachesize"+cachesize);
                    
                }
            }});
         
         
         
         } catch (Exception e) {
            e.printStackTrace();
        }

/*        Method[] methods = PackageManager.class.getMethods();
        for (int i = 0; i < methods.length; i++) {
            if ("getPackageSizeInfo".equals(methods[i].getName())) {
                Class<?>[]  paras = methods[i].getParameterTypes();
                for(int j=0;j<paras.length;j++){
                    System.out.println(paras[j]);
                }
            }
        }*/
    }
}

 

IPackageStatsObserver.class});

posted @ 2013-07-29 17:50  Avrilliu  阅读(2007)  评论(0编辑  收藏  举报