简体   繁体   English

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

[英]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. 嗨,我正在尝试清除已安装应用程序的缓存,但已在android 6.0以下版本中实现此功能。但是当我在android 6.0中尝试实现此功能时,它会抛出java.lang.SecurityException:用户和当前进程都没有android.permission .CLEAR_APP_CACHE。 But I have defined it in mainfest.xml. 但是我已经在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. 但是我也读到它由于安全原因无法在android 6.0中完成,但是最流行的应用程序(例如Clean Master)会清除缓存。 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. 在Android M即6.0中,所有权限都必须由用户授予。 So for that you need to declare in manifest.xml. 因此,您需要在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 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: 并在manifest.xml中添加以下代码:

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

Hope it will help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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