package cn.flightfeather.thirdapp.util.updateApp;
|
|
import android.app.DownloadManager;
|
import android.app.job.JobScheduler;
|
import android.app.job.JobService;
|
import android.content.BroadcastReceiver;
|
import android.content.ContentResolver;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.database.Cursor;
|
import android.net.Uri;
|
import android.os.Build;
|
import android.os.Handler;
|
import android.provider.Settings;
|
import android.support.v4.content.FileProvider;
|
|
import java.io.File;
|
import java.io.IOException;
|
|
/**
|
* @author riku
|
* apk更新文件下载完成广播接收器
|
*/
|
public class DownLoadCompleteReceiver extends BroadcastReceiver {
|
private Context context;
|
|
@Override
|
public void onReceive(Context context, Intent intent) {
|
this.context = context;
|
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
|
//在广播中取出下载任务的id
|
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
|
DownloadManager.Query query=new DownloadManager.Query();
|
DownloadManager dm= (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
query.setFilterById(id);
|
Cursor c = dm.query(query);
|
if (c!=null){
|
try {
|
if (c.moveToFirst()){
|
//获取文件下载路径
|
String filename = null;
|
//获取下载状态
|
int status = c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
|
//安卓7.0以上下载文件uri获取方式
|
String fileUri= c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
|
//适配安卓8.0
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (fileUri != null) {
|
filename = Uri.parse(fileUri).getPath();
|
}
|
//授权“安装未知应用”的许可不成功时,需要动态授权
|
if (!context.getPackageManager().canRequestPackageInstalls()) {
|
Intent intent1 = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, Uri.parse("package:" + context.getPackageName()));
|
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
context.startActivity(intent1);
|
}
|
}
|
//适配安卓7.0
|
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
if (fileUri != null) {
|
filename = Uri.parse(fileUri).getPath();
|
}
|
} else {
|
int fileNameIdx = c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
|
filename = c.getString(fileNameIdx);
|
}
|
|
//启动安装
|
if (filename != null) {
|
File apkFile = new File(filename);
|
if (status == DownloadManager.STATUS_SUCCESSFUL) {
|
installApk(apkFile);
|
}
|
}
|
}
|
}catch (Exception e){
|
e.printStackTrace();
|
}finally {
|
c.close();
|
}
|
|
}
|
}
|
}
|
|
private void installApk(File file) {
|
Intent install=new Intent(Intent.ACTION_VIEW);
|
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
//启动更新
|
Uri uri = null;
|
//适配安卓7.0
|
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
|
uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileProvider", file);
|
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
} else {
|
try {
|
Runtime.getRuntime().exec("chmod 777 " + file.getCanonicalPath());
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
uri = Uri.fromFile(file);
|
}
|
|
if (uri!=null){
|
install.setDataAndType(uri,"application/vnd.android.package-archive");
|
context.startActivity(install);
|
}
|
}
|
|
}
|