如何设置闹钟以在特定时间显示祝酒词

时间:2014-04-08 12:08:41

标签: android broadcastreceiver alarmmanager toast alarm

我想根据他的要求从timepicker获取用户的输入,然后我想在那时显示一个Toast消息。我试过以下但是没有用......

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

我的收件人是: -

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

    }

我在manifest中提到了我的接收者名称。如何在特定时间显示吐司。

3 个答案:

答案 0 :(得分:1)

设置闹钟如下:

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

现在将您的接收器设置为清单文件,如:

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

希望这有效。

答案 1 :(得分:0)

你的清单中的

添加:

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

并创建课程AlarmReciever

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

然后在主要活动中添加:

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) {
        }

最后一件事为MainActivity添加check()方法:

public static void check() {
//Compare time and show Toast at particular time
}

答案 2 :(得分:0)

经过一些谷歌搜索后,我得到了我的问题的解决方案。如下所示..参考thisthis

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

    }