Summary of reasons why Android 10 version update fails

I have written a blog about Android version update before, but I don’t know how to write version update. You can read this blog. Today’s blog mainly summarizes the reasons why version update fails. Let’s compare them here. If you don’t see anything written there, add it yourself! !
Android version update blog address: Android detection version update

The following are the reasons why the version update fails:
1. Permissions

<!---->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!---->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <!-- 8.0手机安装软件 -->
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <!-- 允许装载和卸载文件系统权限 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

2. Check whether the read and write permissions are enabled. There is no pop-up box prompting to enable them.

 private static String[] PERMISSIONS = {
    
    
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE };
    private static int REQUEST_PERMISSION_CODE = 1;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    
    
            Log.e(Thread.currentThread().getStackTrace()[2] + "", "checkSelfPermission: " + checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE));
            if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    
    
                requestPermissions(PERMISSIONS, REQUEST_PERMISSION_CODE);
            }
        }
        
        ......
    }
 
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    
    
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION_CODE) {
    
    
            for (int i = 0; i < permissions.length; i++) {
    
    
                Log.e(Thread.currentThread().getStackTrace()[2] + "", permissions[i] + ": " + grantResults[i]);
            }
        }
    }
 

3.
Configuration in data AndroidManifest

  <!-- 7.0安装 -->
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="cn.xu.test.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

Create an xml file under res and create file_paths.xml file under xml file

<paths>
<external-path
    name="files_root"
    path="Android/data/cn.soujianzhu/" />
<external-path
    name="external_storage_root"
    path="." />
</paths>

This should also be done when installing the apk package.

 //安装程序
    protected void installApk(File file) {
    
    

        if (!file.exists()) {
    
    
            Toast.makeText(this, "下载的安装包不存在", Toast.LENGTH_SHORT).show();
            return;
        }
        //判读版本是否在7.0以上 todo 这里是7.0安装是会出现解析包的错误
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    
    

            // todo 在AndroidManifest中的android:authorities值  当前应用的包名:cn.xu.test+FileProvider(数据共享)
            Uri apkUri = FileProvider.getUriForFile(this,
                    "cn.xu.test.FileProvider", file);
            Intent install = new Intent(Intent.ACTION_VIEW);
            install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //添加这一句表示对目标应用临时授权该Uri所代表的文件
            install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            install.setDataAndType(apkUri, "application/vnd.android.package-archive");
            this.startActivity(install);

        } else {
    
    
            Intent install = new Intent(Intent.ACTION_VIEW);
            install.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            this.startActivity(install);
        }

    }

5. This situation usually causes download failure in Android 10.
Add this line android:requestLegacyExternalStorage="true" to the Mainfest Application.

    <manifest ... >
      <!-- This attribute is "false" by default on apps targeting
           Android 10 or higher. -->
      <application android:requestLegacyExternalStorage="true" ... >
        ...
      </application>
    </manifest>
    

Okay, that’s all for now. These are the problems I encountered while coding. If there are other problems in the future, I will continue to update!

Guess you like

Origin blog.csdn.net/qq_42221857/article/details/119320344