Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Clean cache of all applications below API 23 Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 676
    Comment on it

    This blog is for cleaning cache of all application using Reflection on below API leve 23 i.e Marshmallow.

    1. Create Project.

    2. Create package in root folder with name android.content.pm.

    3. Create aidl file int this package with name IPackageDataObserver.aidl

    4. Paste below code to IPackageDataObserver.aidl.

    // IPackageDataObserver.aidl
    package android.content.pm;
    
    // Declare any non-default types here with import statements
    
    oneway interface IPackageDataObserver {
        void onRemoveCompleted(in String packageName, boolean succeeded);
    }

    5. Open Activity Class.

    6. Create Sub class on Activity class with name CachePackageDataObserver.

    7. SubClass is look like:-

     private class CachePackageDataObserver extends IPackageDataObserver.Stub {
            public void onRemoveCompleted(String packageName, boolean succeeded) {
    
            }
        }

    8. Create Instance variable into Activity class.

     private static final long CACHE_APP = Long.MAX_VALUE;
        private CachePackageDataObserver mClearCacheObserver;

    9. Create Method with name clearCache() in Activity.

    10. write below code of clearCache()

       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);
                
                try
                {
                    localMethod.invoke(mPM, localLong, 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();
                }
        
            }
            catch (NoSuchMethodException e1)
            {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

    11. call clearCache() from where you want to clear cache.

    12. Open AndroidManifest.xml file.

    13. Write below permission to Manifest file.

        <uses-permission android:name="android.permission.CLEAR_APP_CACHE" />

    *Note:- This code is useful below Marshmallow devices.

     

    Happy Coding :D

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: