[英]Run apk from inside android application
我在想是否有一种方法可以运行apk文件并从已安装的应用程序内部启动安装过程。
我知道以下代码将打开一个apk文件:
File apkFile = new File(path + file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(apkFile),"application/vnd.android.package-archive");
startActivity(intent);
但是,例如在应用程序文件夹的resources文件夹内时,如何找到apk(path)? 能做到吗?
这将取决于您的手机是否已植根,但是如果没有,则可以将apk文件放入原始文件夹中,打开它,然后运行该意图。 它看起来应该像这样:
File tempFile = Utils.openResourceFile(context, R.raw.yourapk, "yourapkname.apk");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(tempFile),"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Utils.java
public static File openResourceFile(Context context, int resFile, String tempFileName) throws IOException{
InputStream in = context.getResources().openRawResource(resFile);
byte[] b = new byte[in.available()];
in.read(b);
FileOutputStream fout = context.openFileOutput(tempFileName, Context.MODE_WORLD_READABLE);
fout.write(b);
fout.close();
in.close();
return context.getFileStreamPath(tempFileName);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.