我正在构建一个依赖于其他应用程序的Android应用程序,实际上另一个应用程序是我想与该应用程序一起安装的书写板,我试图将apk文件放入原始文件夹中并尝试访问它像这样:
Uri app= Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.app);
可行吗?如果是,我该如何安装这个应用程序?
最佳答案
将您的 apk 放入 asset 文件夹中,并将 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;}
}
关于java - 在android项目中放置并运行apk文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27482878/