Search code examples
androidalarmmanager

can alarmManager.set get for String


 public String getCurrentTime(){
    Calendar c= Calendar.getInstance();
    Log.i("Calander getinstance",Calendar.getInstance()+"");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
    String strDate = sdf.format(c.getTime());
    return strDate;
}

public void alert()
{
      Calendar cal = Calendar.getInstance();
      Intent myIntent = new Intent(MainActivity.this, myReceiver.class);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
      AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
      alarmManager.set(AlarmManager.RTC,Long.parseLong(getCurrentTime()), pendingIntent);
}

In getCurrentTime, It must be return String but

alarmManager.set(AlarmManager.RTC,Long.parseLong(getCurrentTime()), pendingIntent); is only get for Long. Is that Another way. Help Plz.

Solution

  • You cannot parse the formated time back to long.You need to parse the string with your formatter again to get the time.

    So Before setting your time to your alarmmanager you need create another SimpleDateFormat object.

     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
    

    And now

     alarmManager.set(AlarmManager.RTC,(sdf.parse(getCurrentTime())).getTime(), pendingIntent);