2012-08-10 40 views
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()) { 



      } 

     } 

預先感謝您對我的幫助。

回答

3

你會想用Alarm Manager

tutorial是一個非常簡單的例子,如何使用它。

另一個簡單exmaple是將其分解成的

  1. 的基本步驟創建活動
  2. 創建一個BroadcastReceiver來處理該事件

編輯
要通知,請在接收到警報事件時修改廣播接收器以創建通知。

Android Status Notification Documentation很好地顯示了創建通知的基本步驟。

  1. 獲取對NotificationManager
  2. 參考創建一個通知實例。然後,您可以定義圖像,文本和時間,這也只是因爲已經定義的告警事件中的當前時間cliked
  3. 電話通知時,當熄滅
  4. 定義通知的內容,如信息和意圖發動

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

EDIT 2
Creating notification at specified time using Alarm Manager

+0

好吧,我跟着第一個教程,但我發現其中「CTX」不能被解析爲一個變量,getBroadcast不適用於參數的錯誤 – ScorpioBlue 2012-08-10 17:06:29

+0

CTX在該示例引用了應用程序上下文。所以改爲「this」或「YourActivityName.this」 – Nate 2012-08-10 17:12:31

+0

非常感謝你的男人!你知道我怎麼能把它變成通知嗎? – ScorpioBlue 2012-08-10 18:03:48

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是額外數據的名稱,數值1,您發送的值。

希望這會有所幫助!

編輯:

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; 
+0

謝謝你的幫助!在8月17日下午1點這樣的日期,什麼樣的例子可以填補陣列? – ScorpioBlue 2012-08-10 18:06:46

+0

查看我編輯的答案! – 2012-08-10 18:34:56

0
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); 
       } 
+0

如果我使用上面的代碼,它的工作原理......但實際上,我從另一個活動獲取字符串格式的日期:Intent intent = getIntent(); String date = intent.getStringExtra(「name」);這個你能幫我嗎... – user2980181 2013-11-19 18:29:53

相關問題