目前,我每 24 小时在我的网络服务器(而不是 Android 市场)中检查一次后台应用程序版本。如果有更新,它会提示用户下载新的 apk。
Uri uri = Uri.parse(downloadURL);
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
上面的代码打开用户浏览器并开始下载。
我想在不打开浏览器的情况下下载apk文件,或者我需要直接安装最新的apk而不打开其他应用程序(如浏览器)
做这样的事情
// 首先你需要下载apk文件。
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "APPS");
folder.mkdir();
File file = new File(folder, "AnyName."+"apk");
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
/**
* APKURL is your apk file url(server url)
*/
DownloadFile("APKURL", file);
// 下载文件函数是
public void DownloadFile(String fileURL, File directory) {
try {
FileOutputStream f = new FileOutputStream(directory);
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
//c.setDoOutput(true);
c.connect();
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = in.read(buffer)) > 0) {
f.write(buffer, 0, len1);
}
f.close();
} catch (Exception e) {
System.out.println("exception in DownloadFile: --------"+e.toString());
e.printStackTrace();
}
下载apk文件后编写此代码
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/APPS/" + "AnyName.apk")), "application/vnd.android.package-archive");
startActivity(intent);
// 并在清单中授予权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
它可能对你有帮助,我用它来满足你的需要。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)