Android installs and uninstalls another app in one app
1. Create a new folder asserts under app→src→main, and place the apk file you are ready to install in asserts
2. Create a new folder xml under app→src→main→res, right-click the xml folder, select new→XML Resource File, File name is the file name, you can do whatever you want, filepaths in this article; Root element is the resource type, enter paths, confirm and edit the following code:
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:andro> <external-path name="external_files" path="."/> </paths>
3.Edit
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:andro xmlns:tools="/tools" package=""> <uses-permission android:name=".WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name=".READ_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:requestLegacyExternalStorage="true" android:theme="@style/"> <provider android:name="" android:authorities="Bail Name" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name=".FILE_PROVIDER_PATHS" android:resource="@xml/filepaths file in step 2" /> </provider> <activity android:name=".MainActivity"> <intent-filter> <action android:name="" /> <category android:name="" /> </intent-filter> </activity> </application> </manifest>
public class MainActivity extends AppCompatActivity { Context mContext; private Button bt,bt0; private TextView tx; public static boolean isGrantExternalRW(Activity activity) { //Permission determination if (.SDK_INT >= Build.VERSION_CODES.M && ( .WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { (new String[]{ .READ_EXTERNAL_STORAGE, .WRITE_EXTERNAL_STORAGE }, 1); return false; } return true; } @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); mContext = this; tx=findViewById(); bt=findViewById();//Install bt0=findViewById(.button0);//uninstall// (this, ""+().getAbsolutePath(), 0).show(); if(!()){return;}//It is very important to determine whether there is permission to modify it. else{ if(copyApkFromAssets(this, "", ().getAbsolutePath()+"/")){ (new () { @Override public void onClick(View v) { new (mContext) .setIcon(.ic_launcher) .setMessage("Is it installed?") .setPositiveButton("yes", new () { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Intent.ACTION_VIEW); (Intent.FLAG_ACTIVITY_NEW_TASK); // (("file://" + ().getAbsolutePath()+"/"), // "application/-archive"); File apkFile = new File(()+"/"); if (.SDK_INT >= Build.VERSION_CODES.N) { (Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri uri = (, "", apkFile); (uri, "application/-archive"); } else { ((apkFile), "application/-archive"); } (intent); } }).show(); } }); (new () { @Override public void onClick(View v) { Uri packageURI = ("package:.a22");//package:+The package name you want to uninstall Intent intent =new Intent(Intent.ACTION_DELETE); (packageURI); (intent); } }); } }} public boolean copyApkFromAssets(Context context, String fileName, String path) { //Copy the apk file in asserts to the mobile phone storage //You can check from the mobile phone storage to the bottom, it is a separate file, no folder is set boolean copyIsFinish = false; try { InputStream is = ().open(fileName); File file = new File(path); (); FileOutputStream fos = new FileOutputStream(file); int length = (); byte[] temp = new byte[length]; int i = 0; while ((i = (temp)) > 0) { (temp, 0, i); } (); (); copyIsFinish = true; } catch (IOException e) { (); } return copyIsFinish; } }
There are many contradictions in the tutorials on the Internet. I prevent permission errors and set all the permissions that can be added. You can choose to test them when you have time. You can delete some if you should.
This is the end of this article about the sample code of Android installing and uninstalling another app. For more related Android app installation and uninstalling content, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!