调用反射方法时Android中的SecurityException

时间:2012-12-26 11:47:46

标签: java android caching reflection

我正在开发一个Cache-Cleaner应用程序,当我在PackageManager中调用一个隐藏方法时,我得到一个SecurityException,虽然我已经给了我的应用程序使用权限“DELETE_CACHE_FILES”

使用 - 权限:

uses-permission android:name="android.permission.DELETE_CACHE_FILES"
uses-permission android:name="android.permission.CLEAR_APP_CACHE"
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
uses-permission android:name="android.permission.CLEAR_APP_USER_DATA"
uses-permission android:name="android.permission.GET_PACKAGE_SIZE"

反映的方法:

Method method = getPackageManager().getClass().getMethod("deleteApplicationCacheFiles", String.class, IPackageDataObserver.class);

错误:

java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at <MY_PACKAGE_NAME>.MainActivity$1.onClick(MainActivity.java:70)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.SecurityException: Neither user 10037 nor current process has   android.permission.DELETE_CACHE_FILES.
at android.os.Parcel.readException(Parcel.java:1322)
at android.os.Parcel.readException(Parcel.java:1276)
at android.content.pm.IPackageManager$Stub$Proxy.deleteApplicationCacheFiles(IPackageManager.java:2346)
at android.app.ContextImpl$ApplicationPackageManager.deleteApplicationCacheFiles(ContextImpl.java:2562)
... 14 more

第70行:

 method.invoke(getPackageManager(), app.packageName, dataObserver);

它说我没有得到许可,但我已经在我的清单中删除了它......

编辑: 我发现了这个;

W/PackageManager(62): Not granting permission android.permission.DELETE_CACHE_FILES to package <MY_PACKAGE_NAME>.cachecleaner (protectionLevel=3 flags=0x8be46)

EDIT2: deleteApplicationCacheFiles声明(通过method.toGenericString()获取):

public void android.app.ContextImpl$ApplicationPackageManager.deleteApplicationCacheFiles(java.lang.String, android.content.pm.IPackageDataObserver)

4 个答案:

答案 0 :(得分:6)

DELETE_CACHE_FILESsignature|system权限,这意味着它只能由使用固件的签名密钥签名或安装在系统分区上的应用程序保留(例如,通过root设备用户)。普通的SDK应用程序无法保留此权限。

答案 1 :(得分:3)

try to use this i tried this code and it worked for me...

private static final long CACHE_APP = Long.MAX_VALUE;
private CachePackageDataObserver mClearCacheObserver; 
void clearCache() {
    if (mClearCacheObserver == null) {
        mClearCacheObserver = new CachePackageDataObserver();
    }

    PackageManager mPM = getPackageManager();

    @SuppressWarnings("rawtypes")
    final Class[] classes = { Long.TYPE, IPackageDataObserver.class };

    Long localLong = Long.valueOf(CACHE_APP);

    try {
        Method localMethod = mPM.getClass().getMethod(
                "freeStorageAndNotify", classes);

        /*
         * Method localMethod=mPM.getClass()
         * .getDeclaredMethod("deleteApplicationCacheFiles", String.class,
         * IPackageDataObserver.class );
         */
        /*
         * Start of inner try-catch block
         */
        try {
            localMethod.invoke(mPM, localLong, mClearCacheObserver);
            // localMethod.invoke(mPM, packageName, 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();
        }
        /*
         * End of inner try-catch block
         */
    } catch (NoSuchMethodException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

private class CachePackageDataObserver extends IPackageDataObserver.Stub {
    public void onRemoveCompleted(String packageName, boolean succeeded) {
        runOnUiThread(new Runnable() {
            public void run() {

                Toast.makeText(MainActivity.this, "cache memory cleared",
                        5000).show();
            }
        });

        Log.d("cachedel", "cache deleted");
    }// End of onRemoveCompleted() method
}

答案 2 :(得分:1)

你是否引用了这个:

method.setAccessible(true);

答案 3 :(得分:-1)

尝试此功能 freeStorageAndNotify 并预先

<强> android.permission.CLEAR_APP_CACHE