问题描述
首先,我从做这个有帮助 Android的下载二进制文件问题的和Install以编程方式在Android 的应用。谢谢你们!
First of all, I made this with help fromAndroid download binary file problemsand Install Application programmatically on Android. Thanks guys!
我想使自动更新和自动安装一次。这是本地所以它的非市场化的应用程序。
I want to make auto-update and auto-install at once. It is local so it's non-market application.
下面是我的code吧:
Here is my code for it:
public void Update(String apkurl){
try {
URL url = new URL(apkurl);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory() + "/download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "app.apk");
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();//till here, it works fine - .apk is download to my sdcard in download file
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse(PATH+"app.apk"))
.setType("application/android.com.app");
startActivity(promptInstall);//installation is not working
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Update error!", Toast.LENGTH_LONG).show();
}
}
我的权限是网络
, WRITE_EXTERNAL_STORAGE
, INSTALL_PACKAGES
和 DELETE_PACKAGES
。
在意向 promptInstall
加载,应用程序崩溃= /
When Intent promptInstall
is loaded, the app crashes =/
所以,我思念的权限,或者是我的code不正确,或是否有更好的方法来做到这一点?
So, am I missing permissions or is my code incorrect, or is there a better way to do this?
推荐答案
我解决了这个问题。我在使用setData(URI)犯的错误
和的setType(字符串)
。
I solved the problem. I made mistake in setData(Uri)
and setType(String)
.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
这是正确的,现在,我的自动更新的工作。感谢帮助。 =)
That is correct now, my auto-update is working. Thanks for help. =)
这篇关于安卓:安装apk文件编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!