[英]How to clear other applications cache from another android app in Marshmallow?
[英]How to clear cache of other applications in android M (6.0) programmatically
嗨,我正在尝试清除已安装应用程序的缓存,但已在android 6.0以下版本中实现此功能。但是当我在android 6.0中尝试实现此功能时,它会抛出java.lang.SecurityException:用户和当前进程都没有android.permission .CLEAR_APP_CACHE。 但是我已经在mainfest.xml中定义了它。 但是我也读到它由于安全原因无法在android 6.0中完成,但是最流行的应用程序(例如Clean Master)会清除缓存。 如何清理母版清除应用程序的缓存? 请帮我。 我被困在这里。 谢谢
在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"/>
希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.