如何以编程方式清除android M(6.0)中其他应用程序的缓存

时间:2016-04-25 11:58:05

标签: java android caching

您好我正在尝试清除已安装应用程序的缓存,我已经在Android 6.0版本下实现了这一点。但是当我试图在Android 6.0中实现它时,它会抛出java.lang.SecurityException:用户和当前进程都没有android.permission.CLEAR_APP_CACHE。但是我在mainfest.xml中定义了它。但是我也读到它由于安全原因无法在android 6.0中完成,但是像Clean Master这样最流行的应用程序会清除缓存。如何清理主清除应用程序的缓存?请帮我。我卡在这里。感谢名单。

1 个答案:

答案 0 :(得分:1)

在Android M即6.0中,用户必须授予所有权限。因此,您需要在manifest.xml中声明。

如需更多参考,请查看以下链接,该链接将指导您如何授予用户权限。

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

您需要通过添加以下代码来更新代码:

 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;
            }

然后覆盖以下方法:

 @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
        }
    }

并在manifest.xml中添加以下代码:

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

希望它会对你有所帮助。