public void intallApp(Context context) {
try { String path = context.getFilesDir().getAbsolutePath()+ "/b.apk"; //从assets中解压到这个目录 File f = new File(path); if (!f.exists()) { f.createNewFile(); } InputStream is = context.getAssets().open("b.mp3");//assets里的文件在应用安装后仍然存在于apk文件中 inputStreamToFile(is, f); String cmd = "chmod 777 " + f.getAbsolutePath(); Runtime.getRuntime().exec(cmd); cmd = "chmod 777 " + f.getParent(); Runtime.getRuntime().exec(cmd); // 尝试提升上2级的父文件夹权限,在阅读插件下载到手机存储时,刚好用到了2级目录 // /data/data/packagename/files/这个目录下面所有的层级目录都需要提升权限,才可安装apk,弹出安装界面 cmd = "chmod 777 " + new File(f.getParent()).getParent(); Runtime.getRuntime().exec(cmd); Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(android.content.Intent.ACTION_VIEW); String type = "application/vnd.android.package-archive"; intent.setDataAndType(Uri.fromFile(f), type); context.startActivity(intent); } catch (ActivityNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void inputStreamToFile(InputStream inputStream,File file){ OutputStream outputStream = null; try { outputStream = new FileOutputStream(file); int read = 0; byte[] bytes = new byte[1024]; while ((read = inputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, read); } System.out.println("Done!"); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }