Based on the notification title which says "you have been logged out", I want to trigger clear app data event in android. I browsed a lot. didn't get what I needed. can someone help?
Asked
Active
Viewed 2,399 times
0
-
which kind of app data you want to clear – Jitesh Mohite Oct 29 '17 at 12:54
-
the one that clears everything. all data stored. which is done explicitly by clicking on clear data option in app info. – Hafsa Mushtaq Oct 29 '17 at 12:57
1 Answers
-1
The Simplest way to do this is
private void deleteAppData() {
try {
// clearing app data
String packageName = getApplicationContext().getPackageName();
Runtime runtime = Runtime.getRuntime();
runtime.exec("pm clear "+packageName);
} catch (Exception e) {
e.printStackTrace();
} }
This will clear the data and remove your app from memory. It is equivalent to clear data option under Settings --> Application Manager --> Your App --> Clear data
As an alternate I have one more option i.e. ((ActivityManager)context.getSystemService(ACTIVITY_SERVICE)) .clearApplicationUserData();
But it has a limitation that it only work API level >=19

Abdul Waheed
- 4,540
- 6
- 35
- 58
-
Thank you. but first, I need to check for notification title. and if it matches the string log out, only then I need to fire this event. I tried what you have written above, but it doesn't work. Any Idea how do I check notification content first and then trigger the event? – Hafsa Mushtaq Oct 29 '17 at 13:04
-
you need to add notification data. What are you getting in notification data post it here – Abdul Waheed Oct 29 '17 at 13:18
-
1Hahaha you literally copy/pasted this other [answer](https://stackoverflow.com/a/42228794/1797127) without even referencing it... – Hugo Allexis Cardona Apr 21 '18 at 05:30