Reflecting methods to clear Android app cache

一世执手 提交于 2020-01-03 05:32:28

问题


I am trying to clear the app cache of other android apps besides my own. To do this, I am using reflection on the PackageManager class. However, whenever I initialize the method before I invoke it, it always ends up being 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. Any suggestions?


回答1:


Take a look at the docs for Security on 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.

It sounds like the system will block you from doing this (through reflection too).



来源:https://stackoverflow.com/questions/10262998/reflecting-methods-to-clear-android-app-cache

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