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.
Asked
Active
Viewed 3,119 times
0
-
Hi guys, I got the hack that is used by other apps to clear cache memory in android M (6.0). Now I am able to clear cache memory in android M. Thanks for replying guys.. Thanks again – Jitendra Singh Apr 29 '16 at 06:33
-
@jitendar Sign can you share what was the solutions. – silentsudo Oct 19 '16 at 08:22
-
@sector11 they uses accessibility service to do it... – Jitendra Singh Oct 27 '16 at 09:49
-
@JitendraSingh Can you please answer https://stackoverflow.com/questions/56206710/clearing-the-cache-on-os-versions-above-marshmallow question. I have started a bounty worth 100 points. – Rahulrr2602 May 27 '19 at 17:10
-
Did anyone get any solution? – Bhaven Shah Dec 15 '21 at 07:36
1 Answers
1
In Android M i.e 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.
KishuDroid
- 6,116
- 4
- 30
- 46
-
1But Still he has a point.. How come Clean master clears the data.. Without asking for permission.. – Sanoop Surendran Apr 25 '16 at 12:04
-
I also do that. requesting permission at run time. but still facing same problem.. – Jitendra Singh Apr 25 '16 at 12:07
-
@Sanoop: I have marshmallow device and it ask for every permission. – KishuDroid Apr 25 '16 at 12:54
-
@KishDroid: I used this link to clear cache, this works below android M http://stackoverflow.com/a/17334600/4845434 – Jitendra Singh Apr 26 '16 at 05:53
-
-
@KishDroid: Have you tried this? I also tried this but it returns permission not granted.. – Jitendra Singh Apr 26 '16 at 09:45
-
I have tried this and it's working absolutely fine in my code. Have you define both the permission in manifest? – KishuDroid Apr 26 '16 at 09:56
-
@KishDroid: It just showing a toast "You need to accept the permission". Yes I have defined both permissions in manifest. – Jitendra Singh Apr 26 '16 at 11:38
-
@KishDroid: and Sanoop: can we do it using AccessibiltyService, I have noticed that when we first time clear junk in Clean Master application it requests for accessibiltyService. How does Clean Master do it? – Jitendra Singh Apr 26 '16 at 11:41
-
@JitendraSingh: Go on settings->app->your app->permission->approve your permission – KishuDroid Apr 26 '16 at 12:14
-
@KishuDroid: There is no option for CLEAR_APP_CACHE permission there. – Jitendra Singh Apr 26 '16 at 12:17
-
@JitendraSingh: see this: http://stackoverflow.com/questions/31989637/android-m-reflection-method-freestorageandnotify-exception – KishuDroid Apr 26 '16 at 12:20
-
@KishuDroid: I have used all the brute force which I have found on web to clear cache in android M. I guess It can be happen by accessibilityService only. – Jitendra Singh Apr 26 '16 at 12:40
-
@JitendraSingh have you done with clear cache? I am also working on it and facing the same issue as you. can you help me? – Bhaven Shah Dec 15 '21 at 07:19
-
@KishuDroid I have tried your solution but unfortunately, it's not working. – Bhaven Shah Dec 15 '21 at 07:19