Android app检测版本并下载apk后实现覆盖安装
请问这个功能怎么做呢 大家是用的官方的DownloadManager 吗 为啥我这能下载就是不能提示安装 哪位大神有好的方法 确实挺急的 在线等!
/**
* 安装apk
*/
public static void installApk(Context context,String fileName)
{
File file = new File(fileName);
chmod("777", file.getAbsolutePath());
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
String type = "application/vnd.android.package-archive";
intent.setDataAndType(Uri.fromFile(file), type);
context.startActivity(intent);
}
用DownloadManager下载并 注册广播及时监听下载完成状态 receiver = new DownloadCompleteReceiver();
mContext.registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));,在广播接收处里调用installApk方法,还可以定制相关提示信息
class DownloadCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
mDownloadCustomViewDialog.dismiss();
AppCookie.getInstance().putDownloadVersion(version);
DialogUtil.showToastDialog(mContext, "下载成功...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
AndroidSystemInfo.installApk(mContext, apkPath);
if (receiver != null){
mContext.unregisterReceiver(receiver);
BaseActivity activity = (BaseActivity) mContext;
activity.finish();
}
}
}
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.setDestinationInExternalPublicDir("ChinaCloudproviders",
"ChinaCloudproviders.apk");
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE
| DownloadManager.Request.NETWORK_WIFI);
request.setShowRunningNotification(false);
request.setVisibleInDownloadsUi(false);
request.setMimeType("application/vnd.android.package-archive");
三个路径:
public final static String PATH = Environment.getExternalStorageDirectory().getAbsolutePath()+"";//sd卡根目录路径
private String dirPath = AndroidSystemInfo.PATH + "/ChinaCloudproviders";
private String apkPath = AndroidSystemInfo.PATH + "/ChinaCloudproviders/ChinaCloudproviders.apk";