繁体   English   中英

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

[英]placing and running apk file inside android project

我正在构建一个依赖于另一个应用程序的android应用程序,实际上另一个应用程序是我要与该应用程序一起安装的书写板,我试图将apk文件放在原始文件夹中并尝试像这样访问它:

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

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

将您的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;}

}

试试(我想这个问题已经在这里回答了: 在Android上以编程方式安装Application

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM