【问题标题】:android alarmmanager + task scheduleandroid报警管理器+任务调度
【发布时间】:2012-12-09 19:25:10
【问题描述】:

我需要帮助。我是 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
}

【问题讨论】:

  • 您好,您需要在这里询问具体问题。就像我做了这个和这个,我运行了代码,我得到了这个错误,所以我的问题是这样的。

标签: android task alarmmanager


【解决方案1】:

看看下面的代码:

    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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多