android动态加载未安装的apk文件中…

来源:互联网 发布:大数据教程 pdf 编辑:程序博客网 时间:2024/06/10 07:53
@Override
 protected void onCreate(BundlesavedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 
  try {
   File newFile= new File(getFilesDir(),"jky_4_listView.apk");
   File newFile1= new File(getFilesDir(),"jky_4_listView_temp.apk");
   
   //newFile.getAbsolutePath()表示要动态加载的apk文件,newFile1.getAbsolutePath()表示为了优化临时产生的apk文件(由系统处理)
   
   DexFiledexFile =dalvik.system.DexFile.loadDex(newFile.getAbsolutePath(),newFile1.getAbsolutePath(),0);
   //装载apk文件中的类com.gyd.listview.Test,并创造com.gyd.listview.Test该类的对象实例
   Object obj =dexFile.loadClass("com.gyd.listview.Test",getClassLoader()).newInstance();
   //使用java反射技术获取execute方法的Method对象
   Method method= obj.getClass().getMethod("execute", null);
   String result= String.valueOf(method.invoke(obj, null));
   System.out.println("====result:"+result);
  } catch (Exception e) {
    e.printStackTrace();
  }
 }
0 0
原创粉丝点击