简体   繁体   English

反映清除Android应用程序缓存的方法

[英]Reflecting methods to clear Android app cache

I am trying to clear the app cache of other android apps besides my own. 我正在尝试清除我自己以外的其他Android应用程序的应用程序缓存。 To do this, I am using reflection on the PackageManager class. 为此,我在PackageManager类上使用了反射。 However, whenever I initialize the method before I invoke it, it always ends up being null. 但是,每当我在调用该方法之前初始化该方法时,它总是以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);

Here is where my problem is: 这是我的问题所在:

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

Because Method deleteApplicationCacheFiles is null, my toast message shows up. 因为方法deleteApplicationCacheFiles为null,所以出现了我的吐司消息。 Any suggestions? 有什么建议么?

Take a look at the docs for Security on Android: http://developer.android.com/guide/topics/security/security.html 看看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. Android安全体系结构的中心设计要点是,默认情况下,没有应用程序有权执行任何会对其他应用程序,操作系统或用户产生不利影响的操作。 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. 这包括读取或写入用户的私人数据(例如联系人或电子邮件),读取或写入其他应用程序的文件,执行网络访问,使设备保持唤醒状态等。

It sounds like the system will block you from doing this (through reflection too). 听起来系统会阻止您执行此操作(也通过反射)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM