Android Clear data app
I m working on an android app. I m storing some user info in shared preference. I w开发者_StackOverflow社区ant user should not be able to clear data of app from settings. can it be possible?
There is one permission in Manifest CLEAR_APP_USER_DATA can i use it?
I want user should not be able to clear data of app from settings. can it be possible?
No, sorry. Users can do whatever they want with their phones, including clearing all their data, uninstalling your application, etc.
I haven't tried it, and I know this is an old question, but you could backup app data on google servers (tutorial here: http://developer.android.com/guide/topics/data/backup.html).
And then technically the user could still clear the data, but you could monitor it using an alarm.
So, whenever the user exists your app, you could start a service to backup the user data using the tutorial above and also persist a value to sharedPreferences to say that the data is backed up as follows:
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences();//or name your own shared pref file
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("dataIsBackedUp", true);
Then make an alarm to fire in 30 seconds or 1 minute or so that will send a broadcast to a broadcastReceiver class to check the value of dataIsBackedUp and then restore the data if dataIsBackedUp returns false.
To set the alarm: (You would do this in the app itself and in the broadcast receiver)
Intent checkData = new Intent("MY_CUSTOM_INTENT_FILTER_NAME");
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(),
0, checkData, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmMan = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 1);
alarmMan.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
The BraodcastReceiver to check the data:
public class smsReceived extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent){
SharedPreferences pref = PreferenceManager.getDefault();//or your own named shared prefs file
if (pref.get("dataIsBackedUp", false)){
Intent restoreData = new Intent(context, backupAgent.class);
restoreData.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(restoreData);
}
}
}
And in your manifest:
<receiver android:name=".CheckData">
<intent-filter>
<action android:name="MY_CUSTOM_INTENT_FILTER_NAME"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
There, you specify BOOT_COMPLETED so that you can also check on startup and then start setting alarms again to check every minute, since alarms due not persist through a power cycle
精彩评论