問(wèn)題所在
事情是這樣的,前幾天,發(fā)現(xiàn)有幾個(gè)手機(jī)升級(jí)應(yīng)用失敗,查了下,發(fā)現(xiàn)都是7.0的機(jī)器,于是查了下google的7.0API描述,發(fā)現(xiàn)了這個(gè)東西

google_doc
ps:google的文檔中文化真的很高,現(xiàn)在。
完整解決方案
以前我們是怎么處理下載問(wèn)題的呢?
下載
private static void downloadNewVersionApp(Context context, String url) {
DownloadManager downloadManager =
(DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, getUpdateFile());
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE
| DownloadManager.Request.NETWORK_WIFI);
request.setVisibleInDownloadsUi(false);
long id = downloadManager.enqueue(request);
AppCache.appDownloadId = id;
}
下載完成回調(diào)
private DownloadManager downloadManager;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
if (id != AppCache.appDownloadId) {
return;
}
IntentUtils.installApk(context);
}
}
安裝
public static void installApk(Context context) {
File file= new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
, FishUtils.getUpdateFile());
if (file == null) return;
if (!file.exists()) return;
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
context.startActivity(intent);
}
可以看到以前我們確實(shí)是用URI傳遞地址了,但是現(xiàn)在不行了。所以,要判斷版本
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M){
installAPKGreaterThanM(context,file);
return;
}
7.0 后更新
因?yàn)間oogleAPI的限制,這里我們需要使用FileProvider
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.XXXX.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
exported:要求必須為false,為true則會(huì)報(bào)安全異常。
grantUriPermissions:true,表示授予 URI 臨時(shí)訪問(wèn)權(quán)限。
authorities 組件標(biāo)識(shí),按照江湖規(guī)矩,都以包名開頭,避免和其它應(yīng)用發(fā)生沖突。
看下我們指定的@xml/file_paths
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<paths>
<external-path path="" name="download"/>
</paths>
</resources>
這里的path=""指的是根目錄,即可以其他應(yīng)用共享根目錄和子目錄下的所有資源
恩,然后看下怎么使用
private static void installAPKGreaterThanM(Context context,File file) {
Uri apkUri =
FileProvider.getUriForFile(context, "com.XXXX.fileprovider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
// 由于沒(méi)有在Activity環(huán)境下啟動(dòng)Activity,設(shè)置下面的標(biāo)簽
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//添加這一句表示對(duì)目標(biāo)應(yīng)用臨時(shí)授權(quán)該Uri所代表的文件
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
context.startActivity(intent);
}
大功告成!?。?/p>