这一节了解一下普通应用安装app的方式,主要是唤起系统来安装,直接上代码:
申请权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /><uses-permission android:name="com.example.permission.SOME_PERMISSION" /><uses-permission android:name="android.permission.INSTALL_PACKAGES"tools:ignore="ProtectedPermissions" /><uses-permissionandroid:name="android.permission.WRITE_SETTINGS"tools:ignore="ProtectedPermissions" /><uses-permissionandroid:name="android.permission.ACCESS_SUPERUSER" /><uses-permissionandroid:name=".PERMISSION" /><uses-permissionandroid:name="android.permission.GET_PACKAGE_SIZE" /><uses-permissionandroid:name="android.permission.MANAGE_DOCUMENTS"tools:ignore="ProtectedPermissions" /><application>
...<providerandroid:name="androidx.core.content.FileProvider"android:authorities="com.dc.cplustest.fileProvider"android:exported="false"android:permission="com.example.permission.SOME_PERMISSION"android:grantUriPermissions="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/filepaths" /></provider>
...
</application>
res/xml/filepaths.xml
Activity中:
private static final String[] PERMISSIONS = new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION,android.Manifest.permission.READ_EXTERNAL_STORAGE,android.Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.MANAGE_EXTERNAL_STORAGE,android.Manifest.permission.INSTALL_PACKAGES,Manifest.permission.WRITE_SETTINGS,Manifest.permission.REQUEST_INSTALL_PACKAGES
};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);checkInstallPermission();initView();}public String apk = Environment.getExternalStorageDirectory() + File.separator + "Test" + File.separator + "APK" + File.separator+"fenshen.apk";private void initView() {if (!checkPermissions(PERMISSIONS)) {ActivityCompat.requestPermissions(this, PERMISSIONS, 1000);}findViewById(R.id.btn_test_so).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {try {install();} catch (Exception e) {throw new RuntimeException(e);}}});}public void checkInstallPermission() {boolean haveInstallPermission;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {haveInstallPermission = getPackageManager().canRequestPackageInstalls();Log.d("Test","Test haveInstallPermission:"+haveInstallPermission);if (!haveInstallPermission) {//没有权限让调到设置页面进行开启权限;Uri packageURI = Uri.parse("package:" + getPackageName());Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);startActivityForResult(intent, 10086);} } }public void install() {File apkFile = new File(apk);Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);intent.setAction(Intent.ACTION_VIEW);intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//注意第二个参数,要保持和manifest中android:authorities的值相同Uri uri = FileProvider.getUriForFile(SecondActivity.this,SecondActivity.this.getPackageName() + ".fileProvider", apkFile);intent.setDataAndType(uri, "application/vnd.android.package-archive");intent.setData(uri);startActivity(intent);}private boolean checkPermissions(String[] neededPermissions) {if (neededPermissions == null || neededPermissions.length == 0) {return true;}boolean allGranted = true;for (String neededPermission : neededPermissions) {Log.d("Test","权限"+neededPermission+"是否已给: "+(ContextCompat.checkSelfPermission(this, neededPermission) == PackageManager.PERMISSION_GRANTED));allGranted &= ContextCompat.checkSelfPermission(this, neededPermission) == PackageManager.PERMISSION_GRANTED;}return allGranted;}