我目前在我的网络服务器中每24小时检查一次应用程序版本,而不是在Android市场.如果更新可用,则提示用户下载新的apk.
Uri uri = Uri.parse(downloadURL);
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
上面的代码打开用户浏览器并开始下载.
我想要不打开浏览器我需要下载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);
Run Code Online (Sandbox Code Playgroud)
// DownloadFile函数是
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();
}
Run Code Online (Sandbox Code Playgroud)
并在下载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);
Run Code Online (Sandbox Code Playgroud)
//并在清单中给予许可
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)
它可能对你有所帮助,我根据你的需要使用它.
| 归档时间: |
|
| 查看次数: |
1889 次 |
| 最近记录: |