• 应用安装


    1.ProviderApkActivity

    1. package com.tiger.chapter07_client;
    2. import androidx.annotation.NonNull;
    3. import androidx.annotation.RequiresApi;
    4. import androidx.appcompat.app.AppCompatActivity;
    5. import androidx.core.content.FileProvider;
    6. import android.Manifest;
    7. import android.content.Intent;
    8. import android.content.pm.PackageInfo;
    9. import android.content.pm.PackageManager;
    10. import android.net.Uri;
    11. import android.os.Build;
    12. import android.os.Bundle;
    13. import android.os.Environment;
    14. import android.provider.Settings;
    15. import android.util.Log;
    16. import android.view.View;
    17. import com.tiger.chapter07_client.utils.PermissionUtil;
    18. import com.tiger.chapter07_client.utils.ToastUtlis;
    19. import java.io.File;
    20. public class ProviderApkActivity extends AppCompatActivity implements View.OnClickListener {
    21. private static final String[] PERMISSIONS = new String[]{
    22. Manifest.permission.READ_EXTERNAL_STORAGE
    23. };
    24. private static final int PERMISSION_REQUEST_CODE = 1;
    25. @Override
    26. protected void onCreate(Bundle savedInstanceState) {
    27. super.onCreate(savedInstanceState);
    28. setContentView(R.layout.activity_provider_apk);
    29. findViewById(R.id.btn_install).setOnClickListener(this);
    30. }
    31. @Override
    32. public void onClick(View v) {
    33. // Android 11 之后获取 MANAGE_EXTERNAL_STORAGE 权限
    34. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    35. Log.d("ning", "Android 11+");
    36. checkAndInstall();
    37. } else {
    38. // 如果有权限,直接安装,没有权限则获取权限
    39. if (PermissionUtil.checkPermission(this, PERMISSIONS, PERMISSION_REQUEST_CODE)) {
    40. installApk();
    41. }
    42. }
    43. }
    44. @Override
    45. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    46. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    47. if (requestCode == PERMISSION_REQUEST_CODE &&
    48. PermissionUtil.checkGrant(grantResults)) {
    49. installApk();
    50. }
    51. }
    52. @RequiresApi(api = Build.VERSION_CODES.R)
    53. private void checkAndInstall() {
    54. // 检查是否拥有MANAGE_EXTERNAL_STORAGE 权限,没有则跳转到设置页面
    55. if (!Environment.isExternalStorageManager()) {
    56. Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
    57. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    58. intent.setData(Uri.fromParts("package", getPackageName(), null));
    59. startActivity(intent);
    60. } else {
    61. installApk();
    62. }
    63. }
    64. private void installApk() {
    65. String apkPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/chapter06-release.apk";
    66. Log.d("ning", "apkPath:" + apkPath);
    67. // 获取应用包管理器
    68. PackageManager pm = getPackageManager();
    69. // 获取apk文件的包信息
    70. PackageInfo pi = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
    71. if (pi == null) {
    72. ToastUtlis.show(this, "安装文件已经损坏!");
    73. return;
    74. }
    75. // installer
    76. Uri uri = Uri.parse(apkPath);
    77. // 兼容Android7.0,把访问文件的Uri方式改为FileProvider
    78. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    79. // 通过FileProvider获得文件的Uri访问方式
    80. uri = FileProvider.getUriForFile(this, getString(R.string.file_provider), new File(apkPath));
    81. Log.d("ning", String.format("new uri:%s", uri.toString()));
    82. }
    83. Intent intent = new Intent(Intent.ACTION_VIEW);
    84. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    85. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    86. // 设置Uri的数据类型为APK文件
    87. intent.setDataAndType(uri, "application/vnd.android.package-archive");
    88. // 启动系统自带的应用安装程序
    89. startActivity(intent);
    90. }
    91. }

    2.xml

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:tools="http://schemas.android.com/tools"
    4. android:layout_width="match_parent"
    5. android:layout_height="match_parent"
    6. android:orientation="vertical"
    7. tools:context=".ProviderApkActivity">
    8. <Button
    9. android:id="@+id/btn_install"
    10. android:layout_width="match_parent"
    11. android:layout_height="wrap_content"
    12. android:text="install apk" />
    13. LinearLayout>

    3.清单文件

    1. "1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android">
    3. <uses-feature
    4. android:name="android.hardware.telephony"
    5. android:required="false" />
    6. <queries>
    7. <provider android:authorities="com.tiger.chapter07_server.provider.UserInfoProvider" />
    8. queries>
    9. <uses-permission android:name="android.permission.READ_CONTACTS" />
    10. <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    11. <uses-permission android:name="android.permission.SEND_SMS" />
    12. <uses-permission android:name="android.permission.READ_SMS" />
    13. <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
    14. <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
    15. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    16. <application
    17. android:allowBackup="true"
    18. android:icon="@mipmap/ic_launcher"
    19. android:label="@string/app_name"
    20. android:requestLegacyExternalStorage="true"
    21. android:roundIcon="@mipmap/ic_launcher_round"
    22. android:supportsRtl="true"
    23. android:theme="@style/Theme.MyApplication">
    24. <activity
    25. android:name=".ProviderApkActivity"
    26. android:exported="false" />
    27. <activity
    28. android:name=".ProviderMmsActivity"
    29. android:exported="true">
    30. <intent-filter>
    31. <action android:name="android.intent.action.MAIN" />
    32. <category android:name="android.intent.category.LAUNCHER" />
    33. intent-filter>
    34. activity>
    35. <provider
    36. android:name="androidx.core.content.FileProvider"
    37. android:authorities="@string/file_provider"
    38. android:grantUriPermissions="true">
    39. <meta-data
    40. android:name="android.support.FILE_PROVIDER_PATHS"
    41. android:resource="@xml/file_paths" />
    42. provider>
    43. application>
    44. manifest>

  • 相关阅读:
    [开学季]ChatPaper全流程教程
    台积电的战略布局:“曲线”抢单 | 百能云芯
    基于STM32+腾讯云IO+微信小程序设计的混凝土智能养护系统
    Appium学习日记(二)—— 入门学习(安装Appium和配置环境)
    数据库基本知识
    C++输入输出流解析
    【各种**问题系列】什么是CI&CD(持续集成和持续交付、部署)
    安装 Windows 7 VM虚拟机
    C# 入门—基本语法
    C++list模拟实现
  • 原文地址:https://blog.csdn.net/qq_53374893/article/details/136534238