运行 来自 android 应用程序的 apk

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);

但是,当 apk(路径)位于应用程序文件夹的资源文件夹中时,我如何找到它?可以这样吗?

这取决于您是否已 phone root,但假设没有,您可以将 apk 文件放入 raw 文件夹,打开它然后 运行 那个意图.它应该看起来像这样:

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);     
    }