Uninstall the application (* .apk) after installation

I created an Android app and exported it as *.apk. Can I delete a file automatically apkwith a successful installation?

+1
source share
4 answers

If you install any other APK download through your application and want to uninstall, if after a successful installation follow me :-)

Step 1: declare the following permissions in AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_ADDED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_CHANGED" />    
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_INSTALL" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REPLACED" />

Step 2: Recipient Registration

<receiver
            android:name="com.az.appstore.InstallReceiver"
            android:enabled="true" >
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_CHANGED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <action android:name="android.intent.action.PACKAGE_INSTALL" />


                <data android:scheme="package" />
            </intent-filter>
        </receiver>

Step 4:

In your work with the installer, create a hash map similar to this one I created in ApplicationMain, it will differ according to the implementation.

public static final ConcurrentHashMap<String, File> INSTALL_APK_INFO = new ConcurrentHashMap<String, File>();

and fill it with the name and file of the package in which apk is placed. You can get the name Pacakage from any apk using this method

public static String getPackageNameForAPK(final String archiveFilePath, Context context) {
        try {
            PackageInfo info = context.getPackageManager().getPackageArchiveInfo(archiveFilePath, PackageManager.GET_META_DATA);
            return info.packageName;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }

5:

if (action.equals(Intent.ACTION_PACKAGE_ADDED) || action.equals(Intent.ACTION_PACKAGE_CHANGED) || action.equals(Intent.ACTION_PACKAGE_INSTALL) || action.equals(Intent.ACTION_PACKAGE_REPLACED)) {
            ApplicationInfo info = null;
            try {
                info = context.getPackageManager().getApplicationInfo(intent.getDataString().split(":")[1], PackageManager.GET_META_DATA);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (info != null) {
                    File file = ApplicationMain.INSTALL_APK_INFO.get(info.packageName);
                    if (file != null) {
                        file.delete();
                        ApplicationMain.INSTALL_APK_INFO.remove(info.packageName);
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }

1) - APK-. - APK.

APK

public static void installUpdateAPK(Context context, String pathToAPK) {

        pathToAPK = pathToAPK.replace("file:///", "");

        final Intent intent = new Intent(Intent.ACTION_VIEW);
        File file = new File(pathToAPK);
        intent.setDataAndType(Uri.fromFile(file), APK_MIME_TYPE);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        String name = getPackageNameForAPK(pathToAPK, context);

        if (name != null) {
            ApplicationMain.INSTALL_APK_INFO.put(name, file);
        }
    }

2) , ,

3) , Intent

4) - - delete()

+7

, APK SD- . , APK /data/app/. APK .

, APK . APK / SD-, , .

: SD-

: SD-?

+3

, apk , , . SD-, .

, , , apk , , .

, apks . apk.

- - . , , .

+1

when the application starts, you can check the sdcard / download folder (or another place where you can save apk) manuallly. then remove it from there.

To avoid searching in every run, you can save the first_run field in your general preferences. Set the default value to true and after the first run set the value to false. So you can discover the first runt

-1
source

Source: https://habr.com/ru/post/1795933/


All Articles