简体   繁体   中英

How to clear cache of other applications in android M (6.0) programmatically

Hi I am trying to clear cache of installed applications, I have achieved this below android 6.0 versions.But When While I am trying to achieve it in android 6.0, it throws java.lang.SecurityException: Neither user nor current process has android.permission.CLEAR_APP_CACHE. But I have defined it in mainfest.xml. But I also read it can't be done in android 6.0 due to security reasons, but the most popular applications like Clean Master clears cache. how do clean master clear application's cache? Please help me. I have stuck here. Thanx.

In Android M ie 6.0, all the permission must be granted by the user. So for that you need to declare in manifest.xml.

For more reference you can check below link, which will guide you regarding how the permission can be granted by the user.

http://developer.android.com/training/permissions/requesting.html

You need to update your code by adding below code :

 if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CLEAR_APP_CACHE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                if (ActivityCompat.shouldShowRequestPermissionRationale((IssueItemDetailActivity) mContext,
                        Manifest.permission.CLEAR_APP_CACHE)) {

                    // Show an expanation to the user *asynchronously* -- don't block
                    // this thread waiting for the user's response! After the user
                    // sees the explanation, try again to request the permission.

                    ActivityCompat.requestPermissions(YourActivity.this,
                            new String[]{Manifest.permission.CLEAR_APP_CACHE},
                            1);

                } else {

                    // No explanation needed, we can request the permission.

                    ActivityCompat.requestPermissions(YourActivity.this,
                            new String[]{Manifest.permission.CLEAR_APP_CACHE},
                            1);

                    // 1 is an int constant. The callback method gets the  result of the request.
                }

                return;
            }

Then override the below method :

 @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 1: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay! Do the

                    // contacts-related task you need to do.


                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.

                    Toast.makeText(mContext,"You need to accept the permission",Toast.LENGTH_SHORT).show();
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

And add below code in you manifest.xml:

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

Hope it will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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