2

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

  • 只有一次
  • 日常的
  • 月刊
  • 每年

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

4

2 回答 2

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();

        }
    }
}
于 2013-03-06T10:34:10.470 回答
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) 可以更改为您想要的间隔

希望能帮助到你!

于 2013-03-06T10:20:02.700 回答