String DOWNLOAD_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/Android/data/" + getApplicationContext().getPackageName() + "/files/Download/hospitalManager.apk";
File file = new File(DOWNLOAD_PATH);
if (file.exists()) {
file.delete();
}
DownloadManager downloadManager;
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(BASE_URL + "download"));
//设置在什么网络情况下进行下载
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
//设置通知栏标题
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setTitle("更新院前管理系统");
request.setAllowedOverRoaming(true);
//设置文件存放目录
request.setDestinationInExternalFilesDir(MainActivity.this, Environment.DIRECTORY_DOWNLOADS, "hospitalManager.apk");
downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
long enqueue = downloadManager.enqueue(request);
SharedPreferences usericonpreferencess = getSharedPreferences("xhx", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = usericonpreferencess.edit();
edit.putLong("refernece",enqueue);
edit.commit();
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="xhx.hospitalmanagement.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
属性解释如下:
authorities:是该项目的包名+provider
grantUriPermissions:必须是true,表示授予 URI 临时访问权限
exported:必须是false
resource:中的@xml/file_paths是我们接下来要在资源文件目录下添加的文件
放在application里面。
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="Android/data/xhx.hospitalmanagement/" name="files_root" />
<external-path path="." name="external_storage_root" />
</paths>
其中:
files-path代表的根目录: Context.getFilesDir()
external-path代表的根目录: Environment.getExternalStorageDirectory()
cache-path代表的根目录: getCacheDir()
path 代表要共享的目录
name 只是一个标示,随便取吧 自己看的懂就ok
/**
* 项目名称: android-HospitalManagment
* 类描述:广播,接收是否下载完成
* 创建人:Andministrator
* 创建时间:
* 修改人:Andministrator
* 修改时间:Andministrator
* 修改备注:
*/
public class UpdataBroadcastReceiver extends BroadcastReceiver {
@SuppressLint("NewApi")
public void onReceive(Context context, Intent intent) {
//判断是否下载完成的广播
if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
long myDwonloadID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
SharedPreferences usericonpreferencess = context.getSharedPreferences("xhx", Context.MODE_PRIVATE);
long refernece = usericonpreferencess.getLong("refernece", 0);
Log.i("iii", refernece + "" + myDwonloadID + "");
if (refernece != myDwonloadID) {
return;
}
DownloadManager dManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadFileUri = dManager.getUriForDownloadedFile(myDwonloadID);
installAPK(context, downloadFileUri);
}
}
private void installAPK(Context context,Uri apk ) {
if (Build.VERSION.SDK_INT < 23) {
Intent intents = new Intent();
intents.setAction("android.intent.action.VIEW");
intents.addCategory("android.intent.category.DEFAULT");
intents.setType("application/vnd.android.package-archive");
intents.setData(apk);
intents.setDataAndType(apk, "application/vnd.android.package-archive");
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intents);
} else {
File file = queryDownloadedApk(context);
Log.i("file", file+"");
if (file.exists()) {
openFile(file, context);
}
}
}
/**
* 通过downLoadId查询下载的apk,解决6.0以后安装的问题
* @param context
* @return
*/
public static File queryDownloadedApk(Context context) {
File targetApkFile = null;
DownloadManager downloader = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
SharedPreferences usericonpreferencess = context.getSharedPreferences("xhx", Context.MODE_PRIVATE);
long downloadId = usericonpreferencess.getLong("refernece",-1);
if (downloadId != -1) {
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);
Cursor cur = downloader.query(query);
if (cur != null) {
if (cur.moveToFirst()) {
String uriString = cur.getString(cur.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
if (!TextUtils.isEmpty(uriString)) {
targetApkFile = new File(Uri.parse(uriString).getPath());
}
}
cur.close();
}
}
return targetApkFile;
}
private void openFile(File file, Context context) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri photoURI;
// 判断版本大于等于7.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
photoURI = FileProvider.getUriForFile(context, "xhx.hospitalmanagement.fileprovider", file);
// 给目标应用一个临时授权
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
photoURI = Uri.fromFile(file);
}
intent.setDataAndType(photoURI, "application/vnd.android.package-archive");
context.startActivity(intent);
}
}
<receiver android:name=".Util.UpdataBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
<!--<action android:name="android.intent.action.PACKAGE_INSTALL" />-->
</intent-filter>
</receiver>