0

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.

Harish
  • 3,122
  • 2
  • 31
  • 46

3 Answers3

1

set your alarm like:

Intent intent = new Intent();
        intent.setClass(_Context, OnAlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(_Context, 234324243, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) _Context.getSystemService(Activity.ALARM_SERVICE);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR_OF_DAY, 4);
        cal.add(Calendar.MINUTE, 30);
        cal.add(Calendar.SECOND, 00);

        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                cal.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY,
                pendingIntent);

now set your receiver in menifest file like:

<receiver android:name="com.televero.receiver.OnAlarmReceiver" >
        </receiver>

Hope this works.

jitain sharma
  • 584
  • 5
  • 19
  • same here what you shown and what i written in the above question what is the difference and for your info i mentioned my receiver name in manifest. – Harish Apr 08 '14 at 12:52
  • First use the setInexactRepeating in place of setRepeating, see the Android Developers documentation. Second check your alarm time if it's setting correct and last check if your receiver get called or not. – jitain sharma Apr 08 '14 at 13:53
0

in your manifest add this:

<receiver android:name="Your.Package.Name.AlarmReciever" />

and create class AlarmReciever :

public class AlarmReciever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        MainActivity.check();
    }
}

then in main activity add this:

public static AlarmManager am;
public static PendingIntent sender;
try {

            Intent intent1 = new Intent(thisActivity, AlarmReciever.class);
            sender = PendingIntent.getBroadcast(thisActivity, 2, intent1 ,0);
            am = (AlarmManager) getSystemService(ALARM_SERVICE);
            long l = new Date().getTime();
            if (l < new Date().getTime()) {
                l += 100;
            }
            am.setRepeating(AlarmManager.RTC_WAKEUP, l, 100, sender);
        } catch (Exception e) {
        }

Last thing add check() Method to MainActivity:

public static void check() {
//Compare time and show Toast at particular time
}
Mohammad Rababah
  • 1,730
  • 4
  • 17
  • 32
  • where i need to give my alarm time and what is long l = new Date().getTime();if (l < new Date().getTime()) { l += 100;} this? and why 100 in setrepeating(). – Harish Apr 08 '14 at 12:38
0

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);

    }
Community
  • 1
  • 1
Harish
  • 3,122
  • 2
  • 31
  • 46