导航菜单

页面标题

页面副标题

爱心e站 v1.0.0 - ApkInstallReceiver.java 源代码

正在查看: 爱心e站 v1.0.0 应用的 ApkInstallReceiver.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.newheyd.JZKFcanjiren.Utils.VesionUpdate.receivers;

import android.app.AlertDialog;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;
import android.util.Log;
import com.newheyd.JZKFcanjiren.Utils.VesionUpdate.SpUtils;
import com.newheyd.JZKFcanjiren.View.PhotoSelect.ProviderUtil;
import java.io.File;

public class ApkInstallReceiver extends BroadcastReceiver {
    private static DownloadManager downloadManager;

    @Override
    public void onReceive(Context context, Intent intent) {
        downloadManager = (DownloadManager) context.getSystemService("download");
        if (intent.getAction().equals("android.intent.action.DOWNLOAD_COMPLETE")) {
            long downloadApkId = intent.getLongExtra("extra_download_id", -1L);
            long id = SpUtils.getInstance(context).getLong("downloadId", -1L);
            if (downloadApkId == id) {
                installApk(context, downloadApkId);
            }
        }
    }

    private static void installApk(Context context, long downloadApkId) {
        Intent install = new Intent("android.intent.action.VIEW");
        DownloadManager dManager = (DownloadManager) context.getSystemService("download");
        Uri downloadFileUri = dManager.getUriForDownloadedFile(downloadApkId);
        if (downloadFileUri != null) {
            Log.d("DownloadManager", downloadFileUri.toString());
            File apkFile = queryDownloadedApk(downloadApkId);
            if (apkFile.exists()) {
                if (Build.VERSION.SDK_INT >= 24) {
                    Uri apkUri = FileProvider.getUriForFile(context, ProviderUtil.getFileProviderName(context), apkFile);
                    install.addFlags(1);
                    install.setDataAndType(apkUri, "application/vnd.android.package-archive");
                } else {
                    install.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
                }
                install.addFlags(268435456);
                context.startActivity(install);
                return;
            }
            return;
        }
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setMessage("下载失败,请重新下载");
        dialog.setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog2, int which) {
            }
        });
        dialog.show();
    }

    private static File queryDownloadedApk(long downloadApkId) {
        File targetApkFile = null;
        if (downloadApkId != -1) {
            DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(downloadApkId);
            query.setFilterByStatus(8);
            Cursor cur = downloadManager.query(query);
            if (cur != null) {
                if (cur.moveToFirst()) {
                    String uriString = cur.getString(cur.getColumnIndex("local_uri"));
                    if (!uriString.isEmpty()) {
                        Log.d("执行", "执行");
                        targetApkFile = new File(Uri.parse(uriString).getPath());
                    }
                }
                cur.close();
            }
        }
        return targetApkFile;
    }
}