Android 應(yīng)用內(nèi)下載Apk, 安裝啤它,適配8.0

\color{#008484}{ 下載安裝需要配置的權(quán)限}

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<!--未知來(lái)源安裝權(quán)限 -->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

\color{#008484}{ 一普办、請(qǐng)求存儲(chǔ)權(quán)限}

   /**
    * 我這里用了別人的請(qǐng)求權(quán)限框架 AndPemission
    * Github地址:https://github.com/yanzhenjie/AndPermission
    */
    if (AndPermission.hasPermissions(this, Permission.Group.STORAGE)) {
        // 有存儲(chǔ)權(quán)限, 開(kāi)啟服務(wù)下載
        startService();  
    } else {
       // 無(wú)存儲(chǔ)權(quán)限, 則請(qǐng)求權(quán)限
       AndPermission.with(this).runtime().permission(Permission.Group.STORAGE).onGranted(data -> {
            if (CollectionUtils.isEmpty(data)) {
               return;
            }
            // 用戶(hù)允許了權(quán)限, 開(kāi)啟服務(wù)下載
            startService();      
       }).start();
    }

\color{#008484}{二工扎、啟動(dòng)服務(wù)開(kāi)始下載Apk,注意適配安卓8.0}

   Intent intent = new Intent(this, ApkDownloadService.class);
   // 給Service傳值, 我這里直接把整個(gè)Bean傳過(guò)去了, 其實(shí)只需要個(gè)下載地址和當(dāng)前下載的版本號(hào)
   intent.putExtra(IntentKey.BEAN, newVersionBean);
   if (Build.VERSION.SDK_INT >= 26) {
       // Android8.0適配
       startForegroundService(intent);
   } else {
       startService(intent);
   }

\color{#008484}{在Service的onStartCommand方法中處理下載}

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // android8.0適配: 被啟動(dòng)的Service創(chuàng)建服務(wù)后的五秒內(nèi)要調(diào)用startForground(0, new Notification())
        // 如果不調(diào)用或調(diào)用時(shí)間超過(guò)5秒會(huì)拋出一個(gè)ANR
        // 調(diào)用startForground用到了通知泌豆,android8.0通知又必須要設(shè)置通知渠道
        // 創(chuàng)建通知渠道并運(yùn)行服務(wù)到前臺(tái)
        createNotificationChannel();
        // 獲取Intent傳值信息
        mVersionInfo = (QueryVersionsVo) intent.getSerializableExtra(IntentKey.BEAN);
        // 開(kāi)始異步任務(wù)下載Apk
        downloadApk();
        return super.onStartCommand(intent, flags, START_STICKY);
    }

\color{#008484}{Service的幾個(gè)方法}

    private void createNotificationChannel() {
        // 這里的id輸入自己的項(xiàng)目的包名
        String ID = "com.***.***";
        String NAME = "Channel One";
        Intent intent = new Intent(this, HomeActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        // 創(chuàng)建服務(wù)對(duì)象
        NotificationCompat.Builder notification; 
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // Android8.0要求必須創(chuàng)建通知渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(ID, NAME, NotificationManager.IMPORTANCE_HIGH);
            channel.enableLights(false);
            channel.setShowBadge(false);
            channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            manager.createNotificationChannel(channel);
        }
        notification = new NotificationCompat.Builder(this, ID);
        notification.setContentTitle("更新提示")
                .setContentText("正在下載最新版本..")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher_esp)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_esp))
                .setContentIntent(pendingIntent)
                .build();
        Notification notification1 = notification.build();
        startForeground(1, notification1);
    }


    public void downloadApk() {
        // 設(shè)置最新安裝包名稱(chēng)
        StringBuilder builder = new StringBuilder("your_app_name-");
        if (!TextUtils.isEmpty(mVersionInfo.getPushVersions())) {
            builder.append(mVersionInfo.getPushVersions());
        } else {
            builder.append(System.currentTimeMillis());
        }
        builder.append(".apk");

        // 設(shè)置apk所在目錄
        File baseFile = ContextHolder.getContext().getExternalFilesDir("yc_team");
        if (!baseFile.exists()) {
            baseFile.mkdirs();
        }
        mApkFile = new File(baseFile.getPath(), builder.toString());
        // 最終apk目錄  文件管理-手機(jī)存儲(chǔ)-Android-data-應(yīng)用包名-yc_team-***.apk
        if (mApkFile.exists()) {
            mApkFile.delete();
        }

        // 開(kāi)始異步下載
        mAsyncTask = new DownApkAsyncTask();
        mAsyncTask.execute();
    }

    @SuppressLint("StaticFieldLeak")
    private class DownApkAsyncTask extends AsyncTask<Void, Long, Void> {

        @Override
        protected Void doInBackground(Void... voids) {
            
            HttpURLConnection httpConnection = null;
            InputStream is = null;
            FileOutputStream fos = null;
            int updateTotalSize;
            URL url;
            try {
                url = new URL(mVersionInfo.getUrl());
                httpConnection = (HttpURLConnection) url.openConnection();
                httpConnection.setConnectTimeout(60000);
                httpConnection.setReadTimeout(60000);
                if (httpConnection.getResponseCode() != 200) {
                    return null;
                }
                updateTotalSize = httpConnection.getContentLength();

//                mApkFile.createNewFile();
                is = httpConnection.getInputStream();
                fos = new FileOutputStream(mApkFile, false);
                byte[] buffer = new byte[4096];

                int readSize;
                int currentSize = 0;

                while ((readSize = is.read(buffer)) > 0) {
                    fos.write(buffer, 0, readSize);
                    currentSize += readSize;

                    int finalCurrentSize = currentSize;
                    int finalUpdateTotalSize = updateTotalSize;
                    // 這里可以發(fā)消息實(shí)時(shí)通知進(jìn)度
                    handler.post(() -> {
                        // 用finalCurrentSize和finalUpdateTotalSize計(jì)算出進(jìn)度
                        
                    });
                }
                // 下載完成, 通知安裝
                installApk();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } finally {
                if (httpConnection != null) {
                    httpConnection.disconnect();
                }
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return null;
        }
    }

    /**
     * 我這里是發(fā)了個(gè)EventBus定庵,通知Activity可以安裝Apk了
     */
    private void installApk() {
        handler.post(() -> {
            EventBusMode mode = new EventBusMode(EventBusType.ESP_DOWNLOAD_APK_SUCCESS);
            mode.setTempStr(mApkFile.getAbsolutePath());
            EventBus.getDefault().post(mode);
            // 下載完成,關(guān)閉當(dāng)前服務(wù)
            stopSelf();
        });
    }

    /**
     * 在Service結(jié)束時(shí), 停止異步下載
     */
    @Override
    public void onDestroy() {
        if (mAsyncTask != null) {
            mAsyncTask.cancel(true);
        }
        super.onDestroy();
    }

\color{#008484}{Activity收到通知踪危,開(kāi)始請(qǐng)求未知來(lái)源安裝權(quán)限}

   if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        // Android8.0之前蔬浙,直接安裝Apk
        installApk();
        return;
   }
   boolean haveInstallPermission = getPackageManager().canRequestPackageInstalls();
   if (!haveInstallPermission) {
        // 權(quán)限沒(méi)有打開(kāi)則提示用戶(hù)去手動(dòng)打開(kāi)
        Uri packageURI = Uri.parse("package:" + getPackageName());
        Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
        startActivityForResult(intent, 1001);
   }
    /**
     * 未知來(lái)源安裝權(quán)限申請(qǐng)回調(diào)
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == 1001 && Build.VERSION.SDK_INT >=   Build.VERSION_CODES.O) {
            // 未知來(lái)源安裝應(yīng)用權(quán)限開(kāi)啟
            boolean haveInstallPermission = getPackageManager().canRequestPackageInstalls();
            if (haveInstallPermission) {
                installApk();
            }
        }
    }

    /**
     * 安裝最新Apk
     */
    private void installApk() {
        // Service發(fā)的通知中的文件絕對(duì)路徑
        File file = new File(mNewApkFilePath);
        try {
            // 這里有文件流的讀寫(xiě),需要處理一下異常
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                //如果SDK版本>=24贞远,即:Build.VERSION.SDK_INT >= 24
                String packageName = context.getApplicationContext().getPackageName();
                String authority = new StringBuilder(packageName).append(".provider").toString();
                Uri uri = FileProvider.getUriForFile(context, authority, file);
                intent.setDataAndType(uri, "application/vnd.android.package-archive");
            } else {
                Uri uri = Uri.fromFile(file);
                intent.setDataAndType(uri, "application/vnd.android.package-archive");
            }
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            // 安裝的時(shí)候會(huì)進(jìn)行版本自動(dòng)檢測(cè)畴博,更新版本小于已有版本,是會(huì)走當(dāng)前異常的蓝仲,注意俱病!
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市袱结,隨后出現(xiàn)的幾起案子亮隙,更是在濱河造成了極大的恐慌,老刑警劉巖垢夹,帶你破解...
    沈念sama閱讀 218,204評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件溢吻,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)促王,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)犀盟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人蝇狼,你說(shuō)我怎么就攤上這事阅畴。” “怎么了迅耘?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,548評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵贱枣,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我颤专,道長(zhǎng)冯事,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,657評(píng)論 1 293
  • 正文 為了忘掉前任血公,我火速辦了婚禮,結(jié)果婚禮上缓熟,老公的妹妹穿的比我還像新娘累魔。我一直安慰自己,他們只是感情好够滑,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布垦写。 她就那樣靜靜地躺著,像睡著了一般彰触。 火紅的嫁衣襯著肌膚如雪梯投。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,554評(píng)論 1 305
  • 那天况毅,我揣著相機(jī)與錄音分蓖,去河邊找鬼。 笑死尔许,一個(gè)胖子當(dāng)著我的面吹牛么鹤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播味廊,決...
    沈念sama閱讀 40,302評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼蒸甜,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了余佛?” 一聲冷哼從身側(cè)響起柠新,我...
    開(kāi)封第一講書(shū)人閱讀 39,216評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤畅铭,失蹤者是張志新(化名)和其女友劉穎怠硼,沒(méi)想到半個(gè)月后宙项,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體兰珍,經(jīng)...
    沈念sama閱讀 45,661評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡眶俩,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了伸辟。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片簿姨。...
    茶點(diǎn)故事閱讀 39,977評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖喇嘱,靈堂內(nèi)的尸體忽然破棺而出茉贡,到底是詐尸還是另有隱情,我是刑警寧澤者铜,帶...
    沈念sama閱讀 35,697評(píng)論 5 347
  • 正文 年R本政府宣布腔丧,位于F島的核電站,受9級(jí)特大地震影響作烟,放射性物質(zhì)發(fā)生泄漏愉粤。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評(píng)論 3 330
  • 文/蒙蒙 一拿撩、第九天 我趴在偏房一處隱蔽的房頂上張望衣厘。 院中可真熱鬧,春花似錦压恒、人聲如沸影暴。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,898評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)型宙。三九已至,卻和暖如春伦吠,著一層夾襖步出監(jiān)牢的瞬間妆兑,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,019評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工毛仪, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留搁嗓,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,138評(píng)論 3 370
  • 正文 我出身青樓箱靴,卻偏偏與公主長(zhǎng)得像谱姓,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子刨晴,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容