博客
关于我
Android 版本更新之打开apk文件的前生今世
阅读量:804 次
发布时间:2023-01-23

本文共 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, List
grantedPermissions) {
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.

转载地址:http://wseyk.baihongyu.com/

你可能感兴趣的文章
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>
mysql 添加索引
查看>>
MySQL 添加索引,删除索引及其用法
查看>>
mysql 状态检查,备份,修复
查看>>
MySQL 用 limit 为什么会影响性能?
查看>>
MySQL 用 limit 为什么会影响性能?有什么优化方案?
查看>>
MySQL 用户权限管理:授权、撤销、密码更新和用户删除(图文解析)
查看>>