DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
// 在Notification显示下载进度
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
// 设置Title
request.setTitle("更新");
// 设置描述
request.setDescription("正在下载更新文件...");
private static final String DIR = "AutoUpdate"; private static final String APK = "MyHome.apk"; private static final String PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + DIR + "/" + APK; request.setDestinationInExternalPublicDir(DIR, APK);
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); long id = downloadManager.enqueue(request);
downloadManager.remove(id);
/** * Broadcast intent action sent by the download manager when the user clicks on a running * download, either from a system notification or from the downloads UI. */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public final static String ACTION_NOTIFICATION_CLICKED = "android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
// WIFI状态下下载
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
// 设置通知栏
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
request.setTitle("更新");
request.setDescription("正在下载更新文件...");
// 存放路径
request.setDestinationInExternalPublicDir(DIR, APK);
// 开始下载
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
long id = downloadManager.enqueue(request);
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kongqingwei.downloadmanagerdemo">
<!-- 网络权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_USERS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name=".AutoUpdateBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>
</receiver>
</application>
</manifest>
package com.example.kongqingwei.downloadmanagerdemo;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* Created by kongqingwei on 2016/12/19.
* 广播接收者
*/
public class AutoUpdateBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
Toast.makeText(context, "下载完成", Toast.LENGTH_SHORT).show();
boolean isInstalled = AutoUpdater.installApk();
Toast.makeText(context, isInstalled ? "安装成功" : "安装失败", Toast.LENGTH_SHORT).show();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有