android alarmmanager +任务计划

时间:2012-12-09 19:25:10

标签: android task alarmmanager

我需要帮助。我是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点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重复(例如)新任务,并且当发生这种情况时,它会调用新函数,并使用“profile”变量来处理。

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

1 个答案:

答案 0 :(得分: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