Delete an application (*.apk) after installation

北战南征 提交于 2021-02-18 03:34:09

问题


I've created an android application, and exported it as *.apk. Is it possible to make the apk file to be deleted automatically upon successful installation?


回答1:


If you are installing some other APK downloading through you application and want to remove if after successful install, follow me :-)

Step 1: declare following permissions in your 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: Register Receiver

<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 Installer activity create a hash map like this, I have created in ApplicationMain, it will differ according to implementation.

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

and fill it with package name and file, where apk is put. You can get Pacakage name 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;
        }

    }

Step 5: Now on your install receiver just do this

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();
            }

Summary

1) create a hashmap and populate it with the APK package name and file. File is where exactly your APK is present.

Request to install 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) Register Package added, changed, install intents

3) On your receiver get the the package name that is installed from Intent

4) Get the file object from your hashmap and just call delete()




回答2:


It doesn't matter whether the APK is deleted from the device's SD-Card after the installation is complete. Even if such a function could be programmed in the app, any user with a rooted device will always be able to access the APK from the /data/app/ system partition. Which effectively nullifies your effort to delete the APK in the first place.

That being said, you could add a function that will delete the APK on the first run. Since the APK could be saved / stored just about anywhere on the sd-card, I think these questions should help you with that.

To find a file: Search entire sdcard for a specific file

To delete the file once found: How to delete a file from SD card?




回答3:


If you know where the apk was downloaded to on the device, then you can delete it once installed when your app is first run. If it is on the SD Card, you will need permission to write to it.

However, if you don't know where it is saved, then at best you will have to scan the device for an apk file with the name you gave it and delete it.

Additionally, once installed, apks are copied over to the internal storage. Any user with a rooted device will be able to recover your apk.

Beta testing is a matter of trust. If you do not trust the people who you are giving the apk to for testing, do not give it to them.




回答4:


when the application starts you can check the sdcard/download folder (or other place where you might save the apk) manuallly. then delete that from there.

To avoid searching in every run you can store a first_run field in your shared preference. Set the field true by default and after first run set it false. So that you can detect the first runt



来源:https://stackoverflow.com/questions/15984546/delete-an-application-apk-after-installation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!