
本文共 5236 字,大约阅读时间需要 17 分钟。
AP$
Version Update Workflow in Android Applications
Updating an app to ensure users always have the latest version is a critical feature. Let's explore the process step-by-step.
Android 6.0 (Before Android 7.0)
1. Permissions The app must request the necessary permissions for accessing external storage. xml <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2. APK Download (Using Assets) java private void copyFile() { InputStream inputStream = null; FileOutputStream outputStream = null; try { inputStream = context.getAssets().open("app-debug.apk"); // Get the mounted storage directory String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { File dirFile = new File(Environment.getExternalStorageDirectory() + "/ceshi/"); if (!dirFile.exists()) { dirFile.mkdirs(); } File apk = new File(dir + "app-debug.apk"); if (!apk.exists()) { apk.createNewFile(); } outputStream = new FileOutputStream(apk); byte[] buffer = new byte[1024]; int byteCount = 0; while ((byteCount = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, byteCount); } outputStream.flush(); } } catch (IOException e) { e.printStackTrace(); } finally { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
3. Installing the APK java public static void openFile(Context context, String pathName) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); context.startActivity(intent); }
Android 6.0
1. Runtime Permissions Due to Android 6.0's runtime permissions, the app must dynamically request read and write permissions. java compile 'com.yanzhenjie:permission:1.0.5'
```java if (AndPermission.hasPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { // Request granted, proceed with download copyFile(); } else { // Request permissions AndPermission.with(MainActivity.this) .requestCode(101) .permission(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE) .send(); } ``` ```java @Override protected void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { AndPermission.onRequestPermissionsResult(requestCode, permissions, grantResults, listener); } ``` ```java private PermissionListener listener = new PermissionListener() { @Override public void onSucceed(int requestCode, ListgrantedPermissions) { if (requestCode == 101) { copyFile(); } } @Override public void onFailed(int requestCode, List deniedPermissions) { if (AndPermission.hasAlwaysDeniedPermission MainActivity.this, deniedPermissions)) { AndPermission.defaultSettingDialog(MainActivity.this, 300).show(); } } }; ```
Android 7.0
1. File Sharing Permissions xml <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.tianer.ch.fileprovider" android:exported="false" android:granturipermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>
```xml```
2. Opening APKs java public static void openFile(Context context, String pathName, String authority) { if (pathName == null) { return; } File file = new File(pathName); if (file == null || !file.exists()) { return; } Intent intent = new Intent(Intent.ACTION_VIEW); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setDataAndType(FileProvider.getUriForFile(context, authority, file), "application/vnd.android.package-archive"); } else { intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); } context.startActivity(intent); }
Android 8.0
1. New Installation Permissions xml <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
2. Granting Permissions java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (getPackageManager().canRequestPackageInstalls()) { FileUtil.openFile(context, dir + "app-debug.apk", "com.tianer.ch.fileprovider"); } else { startInstallPermissionSettingActivity(); } }
3. Permission Handling java @RequiresApi(api = Build.VERSION_CODES.O) private void startInstallPermissionSettingActivity() { Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES); startActivityForResult(intent, 10086); }
4. Result Handling java @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 10086) { FileUtil.openFile(context, dir + "app-debug.apk", "com.tianer.ch.fileprovider"); } }
Your feedback and appreciation are what drive me to keep updating this blog. If you found this article useful, please don't forget to like and share it.
发表评论
最新留言
关于作者
