1

我需要帮助。我是 android 编码的新手。

我已经制作了任务清单,我想在任务中写下具体的事情。

这是我的任务项目

private long id;
private int mon;
private int tues;
private int wednes;
private int thurs;
private int fri;
private int satur;
private int sun;
private int profile;

我有几天(星期一,星期二等),其中包含分钟数(对于 10:00,它是 600)。

按照一些教程,我有警报接收器

public class AlarmReciever extends  BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Bundle bundle = intent.getExtras();
            String message = bundle.getString("alarm_message");

            Intent newIntent = new Intent(context, AlarmActivity.class);
            newIntent.putExtra("profile", message);
            newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(newIntent);
        } catch (Exception e) {
            Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
            e.printStackTrace();

        }
}

它仍然未经编辑...

然后有代码调用在警报管理器中执行新任务

// get a Calendar object with current time


Calendar cal = Calendar.getInstance();
 // add 5 minutes to the calendar object
 cal.add(Calendar.MINUTE, 5);
 Intent intent = new Intent(ctx, AlarmReceiver.class);
 intent.putExtra("alarm_message", "O'Doyle Rules!");
 // In reality, you would want to have a static variable for the request code instead of 192837
 PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);

 // Get the AlarmManager service
 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
 am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

我不明白如何在日历中指定,我需要在每个星期一的 10:00 重复(例如)新任务,并且当这种情况发生时,它会调用新函数,并为其提供“配置文件”变量来使用。

 private void setProfile(Integer profile)
{
 // Doing things with profile
}
4

1 回答 1

0

看看下面的代码:

    alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent2 = new Intent(context, SampleAlarmReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(context, 0, intent2, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    // Set the alarm's trigger time to item hour
    calendar.set(Calendar.HOUR_OF_DAY, NuevoItemActivity.hora.getCurrentHour());
    calendar.set(Calendar.MINUTE, NuevoItemActivity.hora.getCurrentMinute());

    // Set the alarm to fire , according to the device's clock, and to repeat once a day.
    alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,  
            calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, (PendingIntent) alarmIntent);

正如您在最后一行中看到的,您可以指示AlarmManager.INTERVAL_DAY重复PendingIntent.

于 2014-01-22T22:28:50.820 回答