Android O 8.0,版本升級不跳轉應用安裝頁面的完美解決

來源:互聯網
上載者:User

標籤:oid   doc   perm   event   mat   data   配置   pac   安裝包   

https://www.jianshu.com/p/af37c1c588c4

最近開發遇到了個問題,app升級的時候,其他手機都能正常升級,下載完安裝包,跳到安裝頁面進行新版本的安裝。但卻有使用者反映,華為P10和華為Mate 9升級時,怎麼也無法跳轉到安裝頁面。起初我以為是華為手機自身系統的問題(因為手上的華為測試機都是正常的),還特地對比了幾種配置的華為手機,最後發現,是Android 8.0系統版本的鍋,不是手機的問題。

2017年8月22日,Google發布了Android 8.0的正式版,其正式名稱為:Android Oreo(奧利奧),Android 8.0強化了許可權管理,變得更加安全。在Android 8.0以前,所有的未知來源應用都可以被安裝,或者會彈窗給使用者一個提示,如此設計雖然方便,但是若被引誘安裝了惡意軟體,安裝"未知來源"的應用有可能會對手機系統帶來潛在的危害;

而在Android 8.0的系統中,未知來源應用許可權的開關被移除掉了,取而代之的是未知來源應用的管理列表,如果你想要安裝某個被自己所信任的開發人員的app,則需要在每一次都手動授權"安裝未知應用"的許可。設定頁面如:(在華為Android 8.0中,開啟該設定頁面:設定列表—>安全與隱私—>更多安全設定—>安裝未知應用)


,若某個應用選擇的是“不允許”,那麼假設app手動升級的時候,就無法成功跳轉到安裝頁面進行正常的App升級流程了,此時需要手動去授權才行,但是很多使用者並不知道需要這麼設定。
廢話說了一大堆,解決其實很簡單。

1.在AndroidManifest.xml檔案中,添加REQUEST_INSTALL_PACKAGES許可權
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
2.在開啟安裝包的代碼中添加相容Android 8.0的代碼。

 /**     * 開啟安裝包     *     * @param mContext     * @param fileUri     */    public void openAPKFile(Activity mContext, String fileUri) {        DataEmbeddingUtil.dataEmbeddingAPPUpdate(fileUri);        // 核心是下面幾句代碼        if (null != fileUri) {            try {                Intent intent = new Intent(Intent.ACTION_VIEW);                File apkFile = new File(fileUri);                //相容7.0                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {                    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);                    Uri contentUri = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".fileProvider", apkFile);                    intent.setDataAndType(contentUri, "application/vnd.android.package-archive");                    //相容8.0                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {                        boolean hasInstallPermission = mContext.getPackageManager().canRequestPackageInstalls();                        if (!hasInstallPermission) {                            ToastUtil.makeText(MyApplication.getContext(), MyApplication.getContext().getString(R.string.string_install_unknow_apk_note), false);                            startInstallPermissionSettingActivity();                            return;                        }                    }                } else {                    intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                }                if (mContext.getPackageManager().queryIntentActivities(intent, 0).size() > 0) {                    mContext.startActivity(intent);                }            } catch (Throwable e) {                e.printStackTrace();                DataEmbeddingUtil.dataEmbeddingAPPUpdate(e.toString());                CommonUtils.makeEventToast(MyApplication.getContext(), MyApplication.getContext().getString(R.string.download_hint), false);            }        }    }    /**     * 跳轉到設定-允許安裝未知來源-頁面     */    @RequiresApi(api = Build.VERSION_CODES.O)    private void startInstallPermissionSettingActivity() {        //注意這個是8.0新API        Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        mContext.startActivity(intent);    }

 
一隻懂音樂的碼蟲
連結:https://www.jianshu.com/p/af37c1c588c4
來源:簡書
 

Android O 8.0,版本升級不跳轉應用安裝頁面的完美解決

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.