Android7.0文件讀寫以及Android8.0安裝apk權限問題


  apk版本更新
  
  Android  7.0 文件讀
  1、動態申請讀寫權限
  
     @TargetApi(23)
    public static boolean checkPermission(AppCompatActivity activity) {
        boolean isGranted = true;
        if (android.os.Build.VERSION.SDK_INT >= 23) {
            if (activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PERMISSION_GRANTED) {
                //如果沒有寫sd卡權限
                isGranted = false;
            }
            if (activity.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PERMISSION_GRANTED) {
                isGranted = false;
            }
            if (activity.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PERMISSION_GRANTED) {
                isGranted = false;
            }
            if (activity.checkSelfPermission(Manifest.permission.CALL_PHONE) != PERMISSION_GRANTED) {
                isGranted = false;
            }
            if (!isGranted) {
                activity.requestPermissions(
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission
                        .ACCESS_FINE_LOCATION,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
                        Manifest.permission.VIBRATE,
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.CALL_PHONE,
                        Manifest.permission.CAMERA},
                    102);
            }
        }
        return isGranted;
    }
    
    2、使用 FileProvider  設置存儲路徑,可兼容7.0以上
    
    附:FileProvider  使用   https://blog.csdn.net/lmj623565791/article/details/72859156
    
    
    Android 8.0   安裝apk  
     
    需要手動配置 安裝權限   
    
    1、靜態配置    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
    2、動態檢查權限
    
    /*
     *
     * 判斷是否是8.0,8.0需要處理未知應用來源權限問題,否則直接安裝
     */
    private void checkIsAndroidO() {
        if (Build.VERSION.SDK_INT >= 26) {
            boolean b = getPackageManager().canRequestPackageInstalls();
            if (b) {
                mMainPresenter.installApk();
            } else {
                //請求安裝未知應用來源的權限
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES},INSTALL_PACKAGES_REQUEST_CODE);
            }
        } else {
            mMainPresenter.installApk();
        }
    }
    
    
    /**
     * 申請權限回調
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case INSTALL_PACKAGES_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    mMainPresenter.installApk();
                } else {
                    //  引導用戶手動開啟安裝權限
                    Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
                    startActivityForResult(intent, GET_UNKNOWN_APP_SOURCES);
                }
                break;
                default:
                    break;

        }
    }
    
    
    
    



    
    

 
          
          

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM