owais khattak
owais khattak

Reputation: 35

How to Clear shared Preference for a Time Picker on Cancel button?

I am Working on a project where i have two buttons and a textview (in layout) for a time picker to set notification, buttonTimepicker & buttonCancelAlarm.

I used to save the Time, selected in the Time Picker by shared Preferences. The shared preferences are working properly.

But the problem i am facing is that, when i click the Cancel button, My app still showing me the notification set by time picker, Even i had implement editor.clear(); on shared preference on click of Cancel button Listner.

When i remove the shared preferences it work properly canceling the time and notification.

Here, what i want is to save the time in a shared preference on Set Time button, And clear shared preference on cancel Time button.

 public class drvschedule extends AppCompatActivity implements 
 TimePickerDialog.OnTimeSetListener {

 private TextView mTextView;
 SharedPreferences myPrefdrv;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drvschedule);

    mTextView = findViewById(R.id.drtmslctd);

    Button buttonTimePicker = findViewById(R.id.setdrvtm);
    buttonTimePicker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment timePicker = new TimePickerFragment();
            timePicker.show(getSupportFragmentManager(), "time picker");
        }
    });

    Button buttonCancelTime = findViewById(R.id.cncldrvtm);
    buttonCancelTime.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            SharedPreferences.Editor editor=myPrefdrv.edit();
            editor.clear();
            editor.apply();
            cancelAlarm();
        }
    });
    myPrefdrv=getPreferences(MODE_PRIVATE);
 }

 @Override
 public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minute);
    c.set(Calendar.SECOND, 0);

    updateTimeText(c);
    startAlarm(c);
 }

 private void updateTimeText(Calendar c) {
    String timeText = "Notification set for Driving: ";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        timeText += 
 DateFormat.getTimeInstance(DateFormat.SHORT).format(c.getTime());
    }

    mTextView.setText(timeText);
 }

 private void startAlarm(Calendar c) {
    AlarmManager alarmManager = (AlarmManager) 
  getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlertReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, 
   intent, 0);

    if (c.before(Calendar.getInstance())) {
        c.add(Calendar.DATE, 1);
    }
    SharedPreferences.Editor editor=myPrefdrv.edit();
    editor.apply();

    alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 
 pendingIntent);
}

private void cancelAlarm() {
    AlarmManager alarmManager = (AlarmManager) 
getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, 
intent, 0);
    alarmManager.cancel(pendingIntent);
    mTextView.setText("Notification canceled");

 }
}



//used to save preferences....
SharedPreferences.Editor editor=myPrefdrv.edit();
editor.apply();

//used to clear preference....
SharedPreferences.Editor editor=myPrefdrv.edit();
editor.clear();
editor.apply();

Upvotes: 0

Views: 242

Answers (2)

Basheer Adel
Basheer Adel

Reputation: 51

You can use this instead of time picker

editors.putLong("lastlogin", new Date().getTime());

this answer

Upvotes: 0

Android Geek
Android Geek

Reputation: 9225

Please try this code To store ,get and clear sharedprefereces data

we have pass two parameters in this method getsharedpreferences(String PREFS_NAME ,int mode )

PREFS_NAME is the name of the file.

mode is the operating mode.

StoreData

 SharedPreferences pref = getSharedPreferences("mypref", 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString("deviceToc",token); // Storing string
        editor.apply();

getData

 SharedPreferences sharedpreferences = getSharedPreferences("mypref", 0);
        SharedPreferences prefs = sharedpreferences;
        String string = prefs.getString("deviceToc", null);

clear data

 SharedPreferences sharedpreferences = getSharedPreferences("mypref", 0);
        sharedpreferences.edit().clear().commit();

Upvotes: 1

Related Questions