2

我正在尝试在某个时间和日期发出警报或通知。我希望能够为数组中的特定日期设置多个警报。我不知道在哪里继续让我的按钮工作:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Button button = (Button)findViewById(R.id.button1);
   // button.setOnClickListener(this);
    final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
    final Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v) {

             if (checkBox.isChecked()) {



             }

        }

预先感谢您帮助我。

4

3 回答 3

3

您将要使用警报管理器

教程是一个非常简单的示例,如何使用它。

另一个简单的例子,将其分解为基本步骤

  1. 创建事件
  2. 创建一个 BroadcastReceiver 来处理事件

编辑
要发出通知,请修改广播接收器以在收到警报事件时创建通知。

Android 状态通知文档很好地展示了创建通知的基本步骤。

  1. 获取对 NotificationManager 的引用
  2. 创建通知实例。然后,您可以定义图像、文本和时间,这只是当前时间,因为警报事件已经定义了何时响起
  3. 定义点击时启动的消息和意图等通知内容
  4. 拨打通知

字符串 ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) receiverContext.getSystemService(ns);

Notification notification = new Notification(R.drawable.notification_icon, "Hello", System.currentTimeMillis());

CharSequence contentTitle = "My notification";  
CharSequence contentText = "Hello World!";  
Intent notificationIntent = new Intent(receiverContext, TargetActivity.class);  
PendingIntent contentIntent = PendingIntent.getActivity(receiverContext, 0, notificationIntent, 0);  

notification.setLatestEventInfo(receiverContext, contentTitle, contentText, contentIntent);  

private static final int notificationID = 1;

mNotificationManager.notify(notificationID, notification);

编辑 2
使用警报管理器在指定时间创建通知

于 2012-08-10T16:32:24.647 回答
1

你可以这样尝试:

ArrayList<Calendar> alarmTimes = getAlarmTimes(); //Make your own function which returns alarm times as an array of Calendar type

for(int i=0; i<alarmTimes.size(); i++)
{
    Intent intent = new Intent(this, YourClass.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTimes[i].getTimeInMillis(), pendingIntent);
}

如果您想独立处理每个警报,请为每个警报使用不同的requestCode。此外,您可能想查看在alarmManager.set()函数中使用的不同标志。根据您的应用程序需要修改它们。

http://developer.android.com/reference/android/app/PendingIntent.html

您将必须创建一个 AlarmReceiver 类,该类将在警报响起时处理您想做的任何事情(确保在 AndroidManifest 中添加此类作为接收器)。如果您想发送额外的信息和警报,那么您可以使用这样的意图附加功能

intent.putExtra("info1", value1);
intent.putExtra("info2", value2);

其中info1是额外数据的名称,而 value1 是您发送的值。

希望这可以帮助!

编辑:

ArrayList<Calendar> alarmTimes = new ArrayList<Calendar>();

Calendar calendar = Calendar.getInstance();
calendar.set(CALENDAR.MONTH, 7);
calendar.set(CALENDAR.DAY_OF_MONTH, 17);
calendar.set(CALENDAR.HOUR_OF_DAY, 13);
calendar.set(CALENDAR.MINUTE, 0);

alarmTimes.add(calendar);

//Repeat the above code for all alarm times and then return array object
return alarmTimes;
于 2012-08-10T17:30:17.757 回答
1
   Calendar cal = Calendar.getInstance(); //retrieves a calendar object w/ current time
        cal.add(cal.MINUTE, 1); //adds 1 minute to current time

    Intent alarmIntent = new Intent(this, CustomAlarmReceiver.class); 

    /* creates a new PendingIntent using the static variable eventID. 
     *  using eventID allows you to create multiple events with the same code
     *  without a unique id the intent would just be updated with new extras each time its created
     */
    PendingIntent pendingAlarm = PendingIntent.getBroadcast(this, eventID, alarmIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT);
    eventID+=1; 

    /* get the Alarm service and set the alarm with the time to go off and what to do when the
     * alarm is received */
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingAlarm);
    Intent intent1=new Intent(getApplicationContext(),ListActivity.class);
    startActivity(intent1);
    intent1.putExtra("date", date);
    intent1.putExtra("task", message);
              }
于 2013-11-19T18:28:13.640 回答