如何在AlarmManager的setRepeating方法中设置值?

时间:2013-03-06 09:47:57

标签: android alarmmanager android-pendingintent

我正在开发一个我必须安排一些活动的应用程序。事件可以按以下顺序发生。

  • 仅限
  • 每日
  • 每月
  • 每年

我正在使用AlarmManager来达到目的,但我对setRepeating AlarmManager方法感到困惑。我输入的内容是time of schedule long,其中包括年,月日期和时间。我如何利用时间来安排我列出的事件?我的意思是我很困惑,我将如何设置在我的时间包括年份的定义时间之后重复事件的时间参数?对此有任何帮助表示赞赏。提前谢谢。

2 个答案:

答案 0 :(得分:0)

查看以下代码

private static final long REPEAT_TIME = 1000 * 10;
AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, YOURStartServiceReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();

// Fetch every 10 seconds
// InexactRepeating allows Android to optimise the energy consumption
service.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), REPEAT_TIME, pending);
// if you dont need InexactRepeating , use this 
//service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), REPEAT_TIME, pending);

(1000 * 10)可以更改为您想要的间隔

希望它有所帮助!

答案 1 :(得分:0)

private void setNotification() {

    // TODO Auto-generated method stub
    getData();
    Calendar cal = Calendar.getInstance();

    long when =0; 
    intent = new Intent(this, AlarmReceiver.class);
    intent.putExtra("eventName", eventName);
    intent.putExtra("eventDescription",eventDescription);

    // Get the AlarmManager service
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

    RadioGroup rg=(RadioGroup)findViewById(R.id.Setting_rgRepeatMode);
    selectedRadio=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
    long repeatTime=0;
    if(selectedRadio.getText().toString().equals("None"))
    {
        cal.set(mYear,mMonth,mDay,mHour,mMinute,0);
        intent.putExtra("Flag",true);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        when=cal.getTimeInMillis();
        am.set(AlarmManager.RTC_WAKEUP,when, pendingIntent);
        return;
    }
    else if(selectedRadio.getText().toString().equals("Daily"))
    {
        intent.putExtra("Flag",true);
        repeatTime=(AlarmManager.INTERVAL_DAY);
    }
    else if(selectedRadio.getText().toString().equals("Weekly"))
    {
        intent.putExtra("Flag",true);
        repeatTime=(AlarmManager.INTERVAL_DAY*7);
    }
    else if(selectedRadio.getText().toString().equals("Monthly"))
    {
        intent.putExtra("month",mDay);
        repeatTime=(AlarmManager.INTERVAL_DAY);
    }
    cal.set(Calendar.HOUR_OF_DAY,mHour);
    cal.set(Calendar.MINUTE,mMinute);
    cal.set(Calendar.SECOND,0);
    when=cal.getTimeInMillis();
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    am.setRepeating(AlarmManager.RTC, when,repeatTime, pendingIntent);

}

另一个类是AlermReceiver.java

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            //              Bundle bundle = intent.getExtras();
            //          String message = bundle.getString("alarm_message");
            //                  Toast.makeText(context,bundle.getString("eventName"), Toast.LENGTH_SHORT).show();

            NotificationManager notificationManager =(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
            int icon = R.drawable.event;
            CharSequence notiText = "Event Notification";
            long time = System.currentTimeMillis();
            @SuppressWarnings("deprecation")
            Notification notification = new Notification(icon, notiText,time);
            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            Intent notificationIntent = new Intent(context, Setting.class);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
            notification.setLatestEventInfo(context,intent.getStringExtra("eventName"),intent.getStringExtra("eventDescription"), contentIntent);
            int SERVER_DATA_RECEIVED =  1;
            Calendar cal=Calendar.getInstance();
            Toast.makeText(context,"aavechhe "+intent.getBooleanExtra("Flag",false),Toast.LENGTH_SHORT).show();
            if(intent.getIntExtra("month",0)==cal.get(Calendar.DAY_OF_MONTH))
            {
                Toast.makeText(context,"aavechhe "+cal.get(Calendar.DAY_OF_MONTH),Toast.LENGTH_SHORT).show();
                notificationManager.notify(SERVER_DATA_RECEIVED, notification);
            }
            if(intent.getBooleanExtra("Flag",false))
                notificationManager.notify(SERVER_DATA_RECEIVED, notification);

        } catch (Exception e) {
            Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
            e.printStackTrace();

        }
    }
}