0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Timothy Miller
  • 291
  • 5
  • 24
  • Hi @TMiller did u find a solution for ur above question? since i am tryng the similar way to force stop and clear data of an app? reply asap thank u in advance – Sudarshan Feb 07 '13 at 06:40

1 Answers1

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

theelfismike
  • 1,621
  • 12
  • 18