在android项目中放置并运行apk文件

时间:2014-12-15 11:19:52

标签: java android uri apk

我正在构建一个取决于其他应用程序的Android应用程序,实际上其他应用程序是我想用应用程序安装的书写板,我试图将apk文件放在raw文件夹中并尝试访问它像这样:

      Uri app= Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.app);

可行吗?如果是,我该如何安装此应用程序?

2 个答案:

答案 0 :(得分:1)

将apk放入资产文件夹,将myAPKFile.apk更改为apk文件名,并使用此功能安装apk。

所需权限:

<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


private boolean InstallmyAPK(){

AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File myAPKFile = new File(Environment.getExternalStorageDirectory().getPath()+"/myAPKFile.apk");

try {

    if(!myAPKFile.exists()){
        in = assetManager.open("myAPKFile.apk");
        out = new FileOutputStream(myAPKFile);

        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }

        in.close();
        in = null;

        out.flush();
        out.close();
        out = null;
    }

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(myAPKFile),
            "application/vnd.android.package-archive");
    startActivity(intent);
    return true;

} catch(Exception e) {return false;}

}

答案 1 :(得分:0)

尝试(我认为这个问题在此之前已经得到了解答:Install Application programmatically on Android

  Intent promptInstall = new Intent(Intent.ACTION_VIEW)
        .setDataAndType(Uri.parse("file:///path/to/your.apk"), 
                        "application/vnd.android.package-archive");
    startActivity(promptInstall);