如何在安卓设备上设置工作日闹钟

6

我有一个情景

设置(周一到周五)的闹钟

假设我选择的时间是:hour = 9,minutes = 15,am_pm = "AM"

现在我想设置每个周一到周五上午9:15的闹钟。

下面是我尝试过的代码,但没有得到预期的结果。

if(choice.equals("Week Days (Mon-Fri)"))
{
    for(int a = 2; a <= 5; a++) //here I am assuming a is from 2 to 5 (calendar DAY_OF_WEEK from Monday to Friday)
    {
        Calendar alarmCalendar = Calendar.getInstance();

        alarmCalendar.set(Calendar.HOUR_OF_DAY, _hourOfDay);

        alarmCalendar.set(Calendar.MINUTE, _minute);

        alarmCalendar.set(Calendar.SECOND, 0);

        alarmCalendar.set(Calendar.MILLISECOND, 0);

        if(am_pm.equals("AM"))
        {
            alarmCalendar.set(Calendar.AM_PM, 0);
        }
        else
        {
            alarmCalendar.set(Calendar.AM_PM, 1);
        }


        alarmCalendar.set(Calendar.DAY_OF_WEEK, a);

        Long alarmTime = alarmCalendar.getTimeInMillis();

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
                alarmTime, 24 * 60 * 60 * 1000 , pendingIntent);
    }

    Toast.makeText(ActivityReminder.this, "Meeting Reminder Set on Week Days (Mon-Fri)", 
                        Toast.LENGTH_LONG).show();
}

我曾经使用BroadcastReceiver,例如:

public class AlarmReceiver extends BroadcastReceiver
{
    NotificationManager mNotificationManager;

    Context context;

    public static final String TAG = "Reminder...";

    @Override
    public void onReceive(Context context, Intent intent)
    {
        this.context = context;

        String subject = "<h3>Meeting Reminder: </h3>" + intent.getStringExtra("subject");

        Toast.makeText(context, Html.fromHtml(subject), Toast.LENGTH_LONG).show();

        showNotification(subject);
    }

    @SuppressWarnings("deprecation")
    private void showNotification(String msg) 
    {
        Intent notificationIntent = new Intent(context.getApplicationContext(),
                ActivityMainScreen.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

        stackBuilder.addParentStack(ActivityMainGeofence.class);

        stackBuilder.addNextIntent(notificationIntent);

        String arrivalTime = TimeUtil.toString(Calendar.getInstance().getTime(), 
                "dd-MM-yyyy hh:mm:ss a");

        notificationIntent.putExtra("subject", msg)
        .putExtra("time", arrivalTime).putExtra("type", "Meetings/Reminder");

        PendingIntent notificationPendingIntent = stackBuilder
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification = new Notification();
        notification.icon = R.drawable.app_icon;

        notification.setLatestEventInfo(context.getApplicationContext(), 
                "WFM Meeting Reminder", Html.fromHtml(msg), notificationPendingIntent);

        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        notification.defaults |= Notification.DEFAULT_SOUND;

        notification.defaults |= Notification.DEFAULT_VIBRATE;

        NotificationManager mNotificationManager = (NotificationManager) 
                context.getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(0, notification);

    }
}

在清单文件中最后一步:

<receiver android:name="com.my_package_name.AlarmReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
</receiver>

编辑: 在这里我想说的是,一次性闹钟对我起作用了,但对于上面的选择,工作日(周一至周五)没有正常工作。

有效代码如下:

if(choice.equals("One Time"))
{
    alarmManager.set(AlarmManager.RTC_WAKEUP, PERIOD, pendingIntent); // here PERIOD is the time selected by me in milliseconds...

    Toast.makeText(ActivityReminder.this, "Meeting Reminder Set One Time", 
            Toast.LENGTH_LONG).show();
}

我做错了什么,请帮忙指点。感激不尽。

4个回答

5
我假设这个闹钟只在周五的9:15被设置?这可能是由于AlarmManager文档中的以下行导致的: 如果已经为相同的IntentSender安排了闹钟,则会首先取消该闹钟。 http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int, long, long, android.app.PendingIntent)
为了达到您想要的效果,您需要5个PendingIntent或者仅设置第一个事件的闹钟,当收到该闹钟时,再设置下一天的闹钟,以此类推。
我可能会选择第二种选项,因为在第一种方法中,您需要5个不同的PendingIntent,这意味着它们的requestCode或backing Intent必须不同(具有不同的类型,操作或类别)。

3
尝试使用这段代码,它对我有效...
    for (int k = 2; k < 5; k++) {
                    int sday = k;
                    Calendar today = Calendar.getInstance();
                    Calendar AlarmDate = Calendar.getInstance();
                    AlarmDate.set(Calendar.HOUR_OF_DAY, inputH);
                    AlarmDate.set(Calendar.MINUTE, inputM);
                    AlarmDate.set(Calendar.SECOND, 0);
                    while (today.after(AlarmDate)) {
                        AlarmDate.add(Calendar.DAY_OF_MONTH, 1);
                    }
                    while (AlarmDate.get(Calendar.DAY_OF_WEEK) != sday) {
                        AlarmDate.add(Calendar.DAY_OF_MONTH, 1);
                    }

                    Intent i = new Intent(this, AlertPopUpActivity.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    i.putExtra("uid", k + (alert.getId() * 1000)); //a unique id for alram
                    i.putExtra("id", alert.getId());
                    i.putExtra("msg", alert.getEventName());

                    if (minute < 10)
                        i.putExtra("time", hour + ":0" + minute);
                    else
                        i.putExtra("time", hour + ":" + minute);
                    i.putExtra("ampm", inputAMPM);
                    PendingIntent pi = PendingIntent.getActivity(this,
                            k + (alert.getId() * 1000), i,         //same unique id (used to update alram if canceled)
                            PendingIntent.FLAG_UPDATE_CURRENT);

                    am.set(AlarmManager.RTC_WAKEUP, AlarmDate.getTimeInMillis(), pi);
                    System.out.println("Set Time :" + AlarmDate.getTime());
                }

2

首先,您不能为同一个待处理意图设置for循环,这将取消先前的实例。

解决方案是在onReceive方法中通过广播接收器再次设置闹钟,在闹钟完成后执行。

以下是如何实现此目的的想法,请根据您的要求包含其余代码。我使用了日历类来设置闹钟,并在星期五跳过两天。

void setAlarm() {
    AlarmManager alarmManager = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    if (choice.equals("One Time")) {
        alarmManager.set(AlarmManager.RTC_WAKEUP, PERIOD, pendingIntent);

        Toast.makeText(context, "Meeting Reminder Set One Time",
                Toast.LENGTH_LONG).show();
    } else if (choice.equals("Week Days (Mon-Fri)")) {
        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_WEEK);

        switch (day) {
        case Calendar.MONDAY:
            alarmManager
                    .set(AlarmManager.RTC_WAKEUP, PERIOD, pendingIntent);
        case Calendar.TUESDAY:
            alarmManager
                    .set(AlarmManager.RTC_WAKEUP, PERIOD, pendingIntent);
        case Calendar.WEDNESDAY:
            alarmManager
                    .set(AlarmManager.RTC_WAKEUP, PERIOD, pendingIntent);
        case Calendar.THURSDAY:
            alarmManager
                    .set(AlarmManager.RTC_WAKEUP, PERIOD, pendingIntent);
        case Calendar.FRIDAY:
            alarmManager.set(AlarmManager.RTC_WAKEUP, PERIOD * 3,
                    pendingIntent);
        }
    }
}

在if-else条件语句中,如果需要使用“choice”变量,您可能需要将这些值存储在类似SharedPreferences的地方。


假设今天是星期一。如果我按照你的代码,那么在Switch-Case中,Calendar.MONDAY将为true,并且alarmManager会在星期一设置它...好的,但是如果明天是星期二呢?如果我不运行应用程序,那么闹钟如何通过Calendar.TUESDAY传递? - Zubair Ahmed
我认为你没有理解我的第二点,当你在onReceive方法中调用showNotification(subject)时,你需要再次在广播接收器类中设置闹钟。你不必运行应用程序,AlarmManager会为你完成。 - Chitrang

2

请查看AlarmManager页面,注意setRepeating()函数的说明。从API 19开始,重复函数是不精确的。要获得正确的重复时间,您需要每次处理闹钟并重新安排它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接