• Android笔记--应用安装


      这一节了解一下普通应用安装app的方式,主要是唤起系统来安装,直接上代码:

     申请权限

    1. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    3. <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
    4. <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    5. <uses-permission android:name="com.example.permission.SOME_PERMISSION" />
    6. <uses-permission android:name="android.permission.INSTALL_PACKAGES"
    7. tools:ignore="ProtectedPermissions" />
    8. <uses-permission
    9. android:name="android.permission.WRITE_SETTINGS"
    10. tools:ignore="ProtectedPermissions" />
    11. <uses-permission
    12. android:name="android.permission.ACCESS_SUPERUSER" />
    13. <uses-permission
    14. android:name=".PERMISSION" />
    15. <uses-permission
    16. android:name="android.permission.GET_PACKAGE_SIZE" />
    17. <uses-permission
    18. android:name="android.permission.MANAGE_DOCUMENTS"
    19. tools:ignore="ProtectedPermissions" />
    20. <application>
    21. ...
    22. <provider
    23. android:name="androidx.core.content.FileProvider"
    24. android:authorities="com.dc.cplustest.fileProvider"
    25. android:exported="false"
    26. android:permission="com.example.permission.SOME_PERMISSION"
    27. android:grantUriPermissions="true">
    28. <meta-data
    29. android:name="android.support.FILE_PROVIDER_PATHS"
    30. android:resource="@xml/filepaths" />
    31. provider>
    32. ...
    33. application>

    res/xml/filepaths.xml

    Activity中:

    1. private static final String[] PERMISSIONS = new String[]{
    2. android.Manifest.permission.ACCESS_COARSE_LOCATION,
    3. android.Manifest.permission.READ_EXTERNAL_STORAGE,
    4. android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
    5. Manifest.permission.MANAGE_EXTERNAL_STORAGE,
    6. android.Manifest.permission.INSTALL_PACKAGES,
    7. Manifest.permission.WRITE_SETTINGS,
    8. Manifest.permission.REQUEST_INSTALL_PACKAGES
    9. };
    10. @Override
    11. protected void onCreate(Bundle savedInstanceState) {
    12. super.onCreate(savedInstanceState);
    13. setContentView(R.layout.activity_second);
    14. checkInstallPermission();
    15. initView();
    16. }
    17. public String apk = Environment.getExternalStorageDirectory() + File.separator + "Test" + File.separator + "APK" + File.separator+"fenshen.apk";
    18. private void initView() {
    19. if (!checkPermissions(PERMISSIONS)) {
    20. ActivityCompat.requestPermissions(this, PERMISSIONS, 1000);
    21. }
    22. findViewById(R.id.btn_test_so).setOnClickListener(new View.OnClickListener() {
    23. @Override
    24. public void onClick(View v) {
    25. try {
    26. install();
    27. } catch (Exception e) {
    28. throw new RuntimeException(e);
    29. }
    30. }
    31. });
    32. }
    33. public void checkInstallPermission() {
    34. boolean haveInstallPermission;
    35. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    36. haveInstallPermission = getPackageManager().canRequestPackageInstalls();
    37. Log.d("Test","Test haveInstallPermission:"+haveInstallPermission);
    38. if (!haveInstallPermission) {//没有权限让调到设置页面进行开启权限;
    39. Uri packageURI = Uri.parse("package:" + getPackageName());
    40. Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
    41. startActivityForResult(intent, 10086);
    42. }
    43. }
    44. }
    45. public void install() {
    46. File apkFile = new File(apk);
    47. Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
    48. intent.setAction(Intent.ACTION_VIEW);
    49. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    50. //注意第二个参数,要保持和manifest中android:authorities的值相同
    51. Uri uri = FileProvider.getUriForFile(SecondActivity.this,
    52. SecondActivity.this.getPackageName() + ".fileProvider", apkFile);
    53. intent.setDataAndType(uri, "application/vnd.android.package-archive");
    54. intent.setData(uri);
    55. startActivity(intent);
    56. }
    57. private boolean checkPermissions(String[] neededPermissions) {
    58. if (neededPermissions == null || neededPermissions.length == 0) {
    59. return true;
    60. }
    61. boolean allGranted = true;
    62. for (String neededPermission : neededPermissions) {
    63. Log.d("Test","权限"+neededPermission+"是否已给: "+(ContextCompat.checkSelfPermission(this, neededPermission) == PackageManager.PERMISSION_GRANTED));
    64. allGranted &= ContextCompat.checkSelfPermission(this, neededPermission) == PackageManager.PERMISSION_GRANTED;
    65. }
    66. return allGranted;
    67. }

  • 相关阅读:
    基于springboot的汽车租赁管理系统的设计与实现
    “开放SDK,创造无限可能”,「四维轻云」又有新功能上线!攻略来了
    FreeSWITCH容器化问题之rtp端口占用
    SSL证书错误怎么办?浏览器常见SSL证书报错解决办法
    vue组件间传参以及方法调用总结
    redis复制机制
    【无标题】
    ROS 学习应用篇(九)ROS中launch文件的实现
    Aspose导出word使用记录
    云导DNS和知识科普以及课堂笔记
  • 原文地址:https://blog.csdn.net/ljt2724960661/article/details/139306583