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*/
    }
}

 

IPackageStatsObserver.class});

转载于:https://www.cnblogs.com/avrilliu/p/3223394.html

你可能感兴趣的:(Android_反射技术_调用类中私有的方法)