java - 反射清除Android应用程序缓存的方法

标签 java android caching reflection delete-file

我正在尝试清除除我自己的应用程序之外的其他 Android 应用程序的应用程序缓存。为此,我在 PackageManager 类上使用反射。但是,每当我在调用方法之前初始化该方法时,它总是以 null 告终。

    private  void initiateClearUserData() {
    // Invoke uninstall or clear user data based on sysPackage
    String thePackageName;
    PackageManager pm = speedy.this.getPackageManager();
    List<ApplicationInfo> installedApps = pm.getInstalledApplications(0);
    ApplicationInfo ai;// = installedApps.get(0);
    ActivityManager.RunningAppProcessInfo process;
    for(int x=0; x<4; x++){
        ai = installedApps.get(x);

这是我的问题所在:

        thePackageName = ai.packageName.toString();// mAppEntry.info.packageName;
        Method deleteApplicationCacheFiles = null;
        mClearCacheObserver = new ClearCacheObserver();
    try {
        deleteApplicationCacheFiles = pm.getClass().getMethod(
             "deleteApplicationCacheFiles", String.class, PackageManager.class);
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 if(deleteApplicationCacheFiles!= null){
     try {
        deleteApplicationCacheFiles.invoke(thePackageName, mClearCacheObserver);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }else{
    Toast.makeText(speedy.this, "Hell naw",
            Toast.LENGTH_SHORT).show();
    }
    }
}

因为方法deleteApplicationCacheFiles为空,所以我的toast消息出现。有什么建议吗?

最佳答案

查看 Android 安全性文档:http://developer.android.com/guide/topics/security/security.html

A central design point of the Android security architecture is that no application, by default, has permission to perform any operations that would adversely impact other applications, the operating system, or the user. This includes reading or writing the user's private data (such as contacts or e-mails), reading or writing another application's files, performing network access, keeping the device awake, etc.

听起来系统会阻止你这样做(也通过反射)。

关于java - 反射清除Android应用程序缓存的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10262998/

相关文章:

Java 流 : List of objects to HashMap without duplicates

android - MPAndroidChart PieChart如何更改中心文本以显示不同的颜色和大小

java - 缓存 XML 提要

java - 使用 Spring Security 加载用户 session

java - 将外部 JAR 添加到 Grails 4.0 应用程序

java - org.springframework.orm.hibernate3.support.BlobByteArrayType 有什么用?

android - ListView 项目状态未在 Android 中保存在滚动中

java - 如何在Firestore中组织相关的时间和固定数据?

asp.net - ASP.NET 中 HTTP 缓存相关 header 的有效含义

java - 具有多个 Java 应用程序的 Hibernate 数据库完整性