安装后删除应用程序(* .apk) [英] Delete an application (*.apk) after installation

查看:138
本文介绍了安装后删除应用程序(* .apk)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个android应用程序,并将其导出为 *.apk .成功安装后是否可以自动删除 apk 文件?

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?

推荐答案

如果您正在通过应用安装其他一些APK下载,并希望在成功安装后将其删除,请跟我来:-)

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

第1步:在您的 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" />

第2步:注册接收者

<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>

第4步:

在您的Installer活动中,创建一个像这样的哈希映射,这是我在 ApplicationMain 中创建的,根据实现的不同,它会有所不同.

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

,并用软件包名称和文件(在其中放置apk)填充它.您可以使用此方法从任何apk获取Pacakage名称

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

    }

第5步:现在,在您的安装接收器上执行此操作

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

摘要

1)创建一个哈希图,并用APK包名称和文件填充它.文件正是您APK所在的位置.

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

请求安装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)注册软件包已添加,更改,安装意图

2) Register Package added, changed, install intents

3)在您的接收器上,获取从 Intent

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

4)从哈希图中获取文件对象,然后调用 delete()

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

这篇关于安装后删除应用程序(* .apk)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆