Harish Harish - 1 month ago 14
Android Question

How to set an alarm to display a toast at particular time

I want to take input from user according to his requirement from timepicker,then i want to display a toast message at that time.,. I tried the following but its not working..

for (int i = 0; i < mNameList.size(); i++) {
Log.i("mName is ", ""
+ mNameList.get(i).getText().toString());
Log.i("Time is ", "" + timeList.get(i).getText().toString());
String[] time = timeList.get(i).getText().toString()
.split(":");
int hour = Integer.parseInt(time[0]);
int min = Integer.parseInt(time[1]);
Log.i("Hour", "" + hour);
Log.i("Min", "" + min);

Intent intent = new Intent(MainActivity.this,
AlarmReciever.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(
MainActivity.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager aManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

Calendar firingcal = Calendar.getInstance();
Calendar currentcal = Calendar.getInstance();

firingcal.set(Calendar.HOUR, hour);
firingcal.set(Calendar.MINUTE, min);

long intendedTime = firingcal.getTimeInMillis();
long currentTime = currentcal.getTimeInMillis();
if (intendedTime >= currentTime) {
aManager.setRepeating(AlarmManager.RTC_WAKEUP,
intendedTime, AlarmManager.INTERVAL_DAY,
alarmIntent);
} else {
firingcal.add(Calendar.DAY_OF_MONTH, 1);
intendedTime = firingcal.getTimeInMillis();

aManager.setRepeating(AlarmManager.RTC_WAKEUP,
intendedTime, AlarmManager.INTERVAL_DAY,
alarmIntent);
}
}


and my reciever is:-

public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Hey its Your turn", Toast.LENGTH_LONG).show();

}


and i mentioned my receiver name in manifest.so how to display toast at a particular time.

Answer

after some googling i got the solution for my question.that is as follows..referring this and this

for (int i = 0; i < mNameList.size(); i++) {
                    Log.i("mName is ", ""
                            + mNameList.get(i).getText().toString());
                    Log.i("Time is ", "" + timeList.get(i).getText().toString());
                    String[] time = timeList.get(i).getText().toString()
                            .split(":");
                    int hour = Integer.parseInt(time[0]);
                    int min = Integer.parseInt(time[1]);
                    Log.i("Hour", "" + hour);
                    Log.i("Min", "" + min);

                    Calendar firingcal = Calendar.getInstance();
                    Calendar currentcal = Calendar.getInstance();

                    firingcal.set(Calendar.MONTH, 3);
                    firingcal.set(Calendar.YEAR, 2014);
                    firingcal.set(Calendar.DAY_OF_MONTH, 9);
                    firingcal.set(Calendar.HOUR_OF_DAY, hour);
                    firingcal.set(Calendar.MINUTE, min);
                    firingcal.set(Calendar.SECOND, 00);

                    if (firingcal.compareTo(currentcal) <= 0) {
                        Toast.makeText(MainActivity.this, "Invalid date/time",
                                Toast.LENGTH_LONG).show();

                    } else {
                        Log.i("Alarm Time in else is  ",
                                "" + firingcal.getTime());
                        scheduleAlarm(firingcal);
                    }

                }
            }


public void scheduleAlarm(Calendar alarmTime) {

        Toast.makeText(MainActivity.this,
                "Alarm time is set at" + alarmTime.getTime(), Toast.LENGTH_LONG)
                .show();
        Intent intent = new Intent(MainActivity.this, AlarmReciever.class);
        final int _id = (int) System.currentTimeMillis();
        PendingIntent pIntent = PendingIntent.getBroadcast(MainActivity.this,
                _id, intent, 0);
        AlarmManager aManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        aManager.set(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(),
                pIntent);

    }