Android7.0自動(dòng)更新適配 包解析異常
在Android7.0的手機(jī)上,自動(dòng)更新的時(shí)候出現(xiàn)包解析異常,在其他的手機(jī)上沒(méi)有這個(gè)問(wèn)題。
原因:
Android7.0引入私有目錄被限制訪問(wèn)和StrictMode API 。私有目錄被限制訪問(wèn)是指在Android7.0中為了提高應(yīng)用的安全性,在7.0上應(yīng)用私有目錄將被限制訪問(wèn)。StrictMode API是指禁止向你的應(yīng)用外公開(kāi) file:// URI。 如果一項(xiàng)包含文件 file:// URI類(lèi)型 的 Intent 離開(kāi)你的應(yīng)用,則會(huì)報(bào)出異常。
解決辦法:
第一步:在AndroidManifest.xml中注冊(cè)provider,provider可以向應(yīng)用外提供數(shù)據(jù)。
<provider
android:authorities="包名.fileprovider"
android:name="android.support.v4.content.FileProvider"
android:grantUriPermissions="true"http://這是設(shè)置uri的權(quán)限
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>//在第二步的時(shí)候會(huì)有介紹
</provider>
第二步:在res/xml中創(chuàng)建file_paths.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path path="" name="download" />
</paths>
</resources>
第三步:貼出我的自動(dòng)更新下載的代碼
public class UpdateManager {
private Context mContext;
private static String savePath ;
private String saveFileName ;
private ProgressBar mProgress; //下載進(jìn)度條控件
private static final int DOWNLOADING = 1; //表示正在下載
private static final int DOWNLOADED = 2; //下載完畢
private static final int DOWNLOAD_FAILED = 3; //下載失敗
private int progress; //下載進(jìn)度
private boolean cancelFlag = false; //取消下載標(biāo)志位
private String serverVersion; //從服務(wù)器獲取的版本號(hào)
private String apkUrl;
// private String apkUrl = "http://liuliu.lejuhuyu.com/AndroidApk/liuliu-dashou-app-1.0.2.apk";
private String clientVersion; //客戶端當(dāng)前的版本號(hào)
private String updateDescription = "請(qǐng)更新當(dāng)前最新版本"; //更新內(nèi)容描述信息
private String forceUpdate; //是否強(qiáng)制更新
private String update;
private VersionBean mVersionBean;
private AlertDialog alertDialog1, alertDialog2; //表示提示對(duì)話框、進(jìn)度條對(duì)話框
public UpdateManager(Context context,VersionBean versionBean) {
this.mContext = context;
this.mVersionBean = versionBean;
apkUrl = "http://liuliu.lejuhuyu.com/AndroidApk/liuliu-dashou-app-"+versionBean.getLastVersion()+".apk";
savePath = Environment.DIRECTORY_DOWNLOADS;
saveFileName = savePath + "/liuliu-dashou-app-"+versionBean.getLastVersion()+".apk";
}
/** 顯示更新對(duì)話框 */
public void showNoticeDialog() {
serverVersion = mVersionBean.getLastVersion();
clientVersion = mVersionBean.getVersion();
L.e("apkUrl="+apkUrl);
L.e("savePath="+savePath);
L.e("saveFileName="+saveFileName);
// forceUpdate = StringUtils.getVersion();
// forceUpdate = "1";
forceUpdate = mVersionBean.getImportant();
update = mVersionBean.getUpdate();
//如果版本最新,則不需要更新
if (serverVersion.equals(clientVersion))
return;
if (update.equals("2"))
return;
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle("發(fā)現(xiàn)新版本 :" + serverVersion);
dialog.setMessage(updateDescription);
dialog.setPositiveButton("現(xiàn)在更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
arg0.dismiss();
showDownloadDialog();
}
});
//是否強(qiáng)制更新
if (forceUpdate.equals("2")) {
dialog.setNegativeButton("待會(huì)更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
arg0.dismiss();
}
});
}
alertDialog1 = dialog.create();
alertDialog1.setCancelable(false);
alertDialog1.show();
}
/** 顯示進(jìn)度條對(duì)話框 */
public void showDownloadDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle("正在更新");
final LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.softupdate_progress, null);
mProgress = (ProgressBar) v.findViewById(R.id.update_progress);
dialog.setView(v);
//如果是強(qiáng)制更新,則不顯示取消按鈕
// if (forceUpdate.equals("1")) {
// dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface arg0, int arg1) {
// // TODO Auto-generated method stub
// arg0.dismiss();
// cancelFlag = false;
// }
// });
// }
alertDialog2 = dialog.create();
alertDialog2.setCancelable(false);
alertDialog2.show();
//下載apk
downloadAPK();
}
DownloadManager manager;
Cursor cursor;
DownloadManager.Request down;
DownloadManager.Query query;
ContentObserver contentObserver;
/** 下載apk的線程 */
public void downloadAPK() {
manager = (DownloadManager) LiuLiuApplication.getContext().getSystemService(Context.DOWNLOAD_SERVICE);
down = new DownloadManager.Request(Uri.parse(apkUrl));
// 設(shè)置允許使用的網(wǎng)絡(luò)類(lèi)型,這里是移動(dòng)網(wǎng)絡(luò)和wifi都可以
down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE
| DownloadManager.Request.NETWORK_WIFI);
// 顯示下載界面
down.setVisibleInDownloadsUi(true);
// 設(shè)置下載路徑和文件名
down.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "liuliu-dashou-app-"+mVersionBean.getLastVersion() + ".apk");
down.setMimeType("application/vnd.android.package-archive");
// 設(shè)置為可被媒體掃描器找到
down.allowScanningByMediaScanner();
down.setAllowedOverRoaming(false);
// down.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
long id = manager.enqueue(down);
query = new DownloadManager.Query().setFilterById(id);
contentObserver = new ContentObserver(mHandler) {
@Override
public void onChange(boolean selfChange) {
// super.onChange(selfChange);
boolean downloading = true;
while(downloading){
cursor = manager.query(query);
try {
if (cursor != null && cursor.moveToFirst()) {
int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
progress = (int) ((bytes_downloaded * 100) / bytes_total);
mHandler.sendEmptyMessage(DOWNLOADING);
if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))==DownloadManager.STATUS_SUCCESSFUL) {
mHandler.sendEmptyMessage(DOWNLOADED);
}else if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))==DownloadManager.STATUS_FAILED){
mHandler.sendEmptyMessage(DOWNLOAD_FAILED);
}
}
}catch (Exception e){
e.printStackTrace();
mHandler.sendEmptyMessage(DOWNLOAD_FAILED);
}finally {
if (cursor != null){
downloading = false;
cursor.close();
}
}
}
}
};
mContext.getContentResolver().registerContentObserver(Uri.parse("content://downloads/"),true,contentObserver);
}
/** 更新UI的handler */
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case DOWNLOADING:
mProgress.setProgress(progress);
break;
case DOWNLOADED:
if (alertDialog2 != null)
alertDialog2.dismiss();
installAPK();
break;
case DOWNLOAD_FAILED:
ToastUtil.getInstance(mContext,"網(wǎng)絡(luò)斷開(kāi),請(qǐng)稍候再試",false).show();
break;
default:
break;
}
}
};
/** 下載完成后自動(dòng)安裝apk */
public void installAPK() {
File apkFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"liuliu-dashou-app-"+mVersionBean.getLastVersion() + ".apk");
if (!apkFile.exists()) {
return;
}
if (Build.VERSION.SDK_INT>=24){
Uri apkUri = FileProvider.getUriForFile(mContext, LiuLiuApplication.getContext().getPackageName()+".fileprovider", apkFile);
Intent install = new Intent(Intent.ACTION_VIEW);
install.addCategory(Intent.CATEGORY_DEFAULT);
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.setDataAndType(apkUri, "application/vnd.android.package-archive");
mContext.startActivity(install);
} else {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("application/vnd.android.package-archive");
intent.setData(Uri.fromFile(apkFile));
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android跨進(jìn)程拋異常的原理的實(shí)現(xiàn)
- Android編程實(shí)現(xiàn)項(xiàng)目中異常捕獲及對(duì)應(yīng)Log日志文件保存功能
- Android Studio升級(jí)到3.0 Terminal 中文顯示異常解決
- Android 全局異常捕獲實(shí)例詳解
- android 捕捉異常并上傳至服務(wù)器的簡(jiǎn)單實(shí)現(xiàn)
- Android Studio下載更新Android SDK網(wǎng)絡(luò)異?;驘o(wú)法下載
- Android 中Crash時(shí)如何獲取異常信息詳解及實(shí)例
- Android異常 java.lang.IllegalStateException解決方法
- AndroidStudio利用android-support-multidex解決64k的各種異常
- 安卓中出現(xiàn)過(guò)的一些容易被忽略的異常整理
相關(guān)文章
Android使用Volley框架定制PostUploadRequest上傳文件
這篇文章主要為大家詳細(xì)介紹了Android使用Volley框架定制PostUploadRequest上傳文件或圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android?性能優(yōu)化實(shí)現(xiàn)全量編譯提速的黑科技
這篇文章主要為大家介紹了Android?性能優(yōu)化實(shí)現(xiàn)全量編譯提速的黑科技,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
關(guān)于Android多渠道打包的進(jìn)階知識(shí)
前一篇文章主要介紹了關(guān)于Android程序的多渠道打包方法,這一篇文章介紹了多渠道打包的進(jìn)階知識(shí),還不會(huì)的同學(xué)快進(jìn)來(lái)學(xué)習(xí)下吧,建議收藏以防迷路2021-08-08
詳解Kotlin Android開(kāi)發(fā)中的環(huán)境配置
這篇文章主要介紹了詳解Kotlin Android開(kāi)發(fā)中的環(huán)境配置的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android StickyListHeaders實(shí)現(xiàn)電話本列表效果
這篇文章主要為大家詳細(xì)介紹了Android StickyListHeaders實(shí)現(xiàn)電話本列表效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
android?studio數(shù)據(jù)存儲(chǔ)建立SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)增刪查改
這篇文章主要介紹了vandroid?studio數(shù)據(jù)存儲(chǔ)建立SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)增刪查改,分別使用sqlite3工具和Android代碼的方式建立SQLite數(shù)據(jù)庫(kù),具體內(nèi)容,需要的小伙伴可以參考下面文章得詳細(xì)內(nèi)容2021-12-12
android實(shí)現(xiàn)快遞跟蹤進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)快遞跟蹤進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05

