Androidダウンロードapkヒント更新およびAndroid 6.0 Marshmallowヒント更新エラー問題の解決
6869 ワード
apkをダウンロードしてインストールする方法はたくさんありますが、GoogleはDownloadManagerを採用することをお勧めします.
更新されたコードをダウンロードするのは簡単で、以下のいくつかのブロックに分けられます.
ダウンロードの開始
そしてダウンロード完了のReceiveを傍受
上記のやり方はAndroid 6.0 Marshmallowシステムの前では問題ありませんが、6.0以降では、エラーが報告され、アプリケーションが直接掛けられます.エラーログは以下の通りです.
Uriの値を確認したところ、6.0は以前とは異なることがわかりました
6.0のdownloadFileUri値は次のとおりです.content://downloads/my_downloads/106.0以前のdownloadFileUri値は次のとおりです.file:///storage/emulated/0/Android/data/com.chiclam.download/files/Download/update-2.apk
グーグルで検索した結果、やっと解決策ができた.次は解決の道だhttp://stackoverflow.com/questions/33315849/android-6-get-path-to-downloaded-file
完全な解決方法:
これにより、6.0上の自動起動更新インタフェースBUGを完璧に解決した.
更新されたコードをダウンロードするのは簡単で、以下のいくつかのブロックに分けられます.
ダウンロードの開始
public long startDownload(String uri, String title, String description) {
DownloadManager.Request req = new DownloadManager.Request(Uri.parse(uri));
req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
//req.setAllowedOverRoaming(false);
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// [ ]
//
//file:///storage/emulated/0/Android/data/your-package/files/Download/update.apk
req.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, "update.apk");
//
//file:///storage/emulated/0/Download/update.apk
//req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "update.apk");
//
//req.setDestinationUri()
//
req.setTitle(title);
req.setDescription(description);
//req.setMimeType("application/vnd.android.package-archive");
return dm.enqueue(req);
}
そしてダウンロード完了のReceiveを傍受
public class ApkInstallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
long downloadApkId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
long id = PrefUtils.getDownloadId();
if (downloadApkId == id) {
installApk(context, downloadApkId);
}
}
}
private void installApk(Context context, long downloadApkId) {
Intent install = new Intent(Intent.ACTION_VIEW);
DownloadManager dManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadFileUri = dManager.getUriForDownloadedFile(downloadApkId);
if (downloadFileUri != null) {
Log.d("DownloadManager", downloadFileUri.toString());
install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(install);
} else {
Log.e("DownloadManager", " ");
}
}
上記のやり方はAndroid 6.0 Marshmallowシステムの前では問題ありませんが、6.0以降では、エラーが報告され、アプリケーションが直接掛けられます.エラーログは以下の通りです.
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content: typ=application/vnd.android.package-archive flg=0x10000000 }
Uriの値を確認したところ、6.0は以前とは異なることがわかりました
6.0のdownloadFileUri値は次のとおりです.content://downloads/my_downloads/106.0以前のdownloadFileUri値は次のとおりです.file:///storage/emulated/0/Android/data/com.chiclam.download/files/Download/update-2.apk
グーグルで検索した結果、やっと解決策ができた.次は解決の道だhttp://stackoverflow.com/questions/33315849/android-6-get-path-to-downloaded-file
完全な解決方法:
private void installApk(Context context, long downloadApkId) {
DownloadManager dManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadApkId);
Cursor c = dManager.query(query);
if(c != null) {
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
String downloadFileUrl = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
startInstall(context, Uri.parse(downloadFileUrl));
}
}
c.close();
}
}
private boolean startInstall(Context context, Uri uri) {
if(!new File( uri.getPath()).exists()) {
System.out.println( " local file has been deleted! ");
return false;
}
Intent intent = new Intent();
intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction( Intent.ACTION_VIEW);
intent.setDataAndType( uri, "application/vnd.android.package-archive");
context.startActivity( intent);
return true;
}
これにより、6.0上の自動起動更新インタフェースBUGを完璧に解決した.