这个问题在这里已经有了答案: Install Application programmatically on Android (18 个回答) 9年前关闭。
我在以下的帮助下做了这个 Android download binary file problems 和 Install Application programmatically on Android .
我想立即进行自动更新和自动安装。它是本地的,所以它是非市场应用。
这是我的代码:
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();
}
}
我的权限是 INTERNET , WRITE_EXTERNAL_STORAGE , INSTALL_PACKAGES , 和 DELETE_PACKAGES .
当 Intent promptInstall 已加载,应用程序崩溃 =/
那么,我是缺少权限还是我的代码不正确,还是有更好的方法来做到这一点?
Best Answer-推荐答案 strong>
我解决了这个问题。我在 setData(Uri) 中犯了错误和 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);
现在是正确的,我的自动更新正在工作。感谢帮助。 =)
2016 年 7 月 20 日编辑:
过了很久,我不得不在另一个项目中再次使用这种更新方式。我遇到了一些旧解决方案的问题。那段时间很多事情都发生了变化,所以我不得不用不同的方法来做这件事。这是代码:
//get destination to update file and set Uri
//TODO: First I wanted to store my update .apk file on internal storage for my app but apparently android does not allow you to open and install
//aplication with existing package from there. So for me, alternative solution is Download directory in external storage. If there is better
//solution, please inform us in comment
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
String fileName = "AppName.apk";
destination += fileName;
final Uri uri = Uri.parse("file://" + destination);
//Delete update file if exists
File file = new File(destination);
if (file.exists())
//file.delete() - test this, I think sometimes it doesnt work
file.delete();
//get url of app on server
String url = Main.this.getString(R.string.update_app_url);
//set downloadmanager
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription(Main.this.getString(R.string.notification_description));
request.setTitle(Main.this.getString(R.string.app_name));
//set destination
request.setDestinationUri(uri);
// get download service and enqueue file
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
//set BroadcastReceiver to install app when .apk is downloaded
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
install.setDataAndType(uri,
manager.getMimeTypeForDownloadedFile(downloadId));
startActivity(install);
unregisterReceiver(this);
finish();
}
};
//register receiver for when .apk download is compete
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
关于Android:以编程方式安装 .apk,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/4967669/
|