2011-12-22 44 views
0

我創建了一個應用程序,根據地理座標計算一天中特定的一組時間。它會在那些時間發出通知。 我已經使用重複鬧鐘通知日常(雖然時間每天改變,因此不準確)。 但它沒有通知我3天后(我沒有觀察到確切的天數),除非重置。爲什麼這樣? 現在我已決定使用服務,將每天設置通知,因此準確。我如何編寫服務並調用它?Android重複報警或服務?

public void alarmNoon(long diff) { 
     Intent intent = new Intent(this, PNoon.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, 
      intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     if (checkadan(1)==true){ 
      if (diff>=0){ 
      am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+diff, 
      (1000*60*60*24), pendingIntent);}else{ 
      am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+diff+(1000*60*60*24), (1000*60*60*24), pendingIntent); 
      }}else{ 
       am.cancel(pendingIntent); 
      } 
    } 

差異是時間differnce btwn中午和當前時間

public class PNoon extends BroadcastReceiver { 
Dialog db; 
@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    String ns = Context.NOTIFICATION_SERVICE; 
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns); 
    Notification notification = new Notification(R.drawable.icon, "Time", System.currentTimeMillis()); 
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0); 
    notification.setLatestEventInfo(context, "Time", "Its Noon", contentIntent);  
    notification.flags=Notification.FLAG_AUTO_CANCEL; 
    notification.sound=Uri.parse("android.resource://net.ttct.time/"+R.raw.NoonAnnouncement); 
    mNotificationManager.notify(1, notification); 

} 
} 

而且聲音停止,一旦通知欄拖動打開,應立即停止,只有當項目在通知欄被觸摸。

<receiver android:process=":remote" android:name="PNoon"></receiver> 
+0

你看過http://developer.android.com/guide/topics/fundamentals/services.html廣播嗎? – THelper 2011-12-22 09:19:33

+1

發佈你的代碼(你如何安排警報),不要忘記你的清單聲明廣播接收機 – 2011-12-22 09:27:21

+0

我希望情況很清楚。警報每天在黎明,中午和黃昏設置通知。我應該實施本地服務還是遠程信使服務?什麼是差異? – JaseemAmeer 2011-12-25 07:34:05

回答

0
 PendingIntent.FLAG_CANCEL_CURRENT change it as intent.FLAG_NEW_TASK 

和登記的manifest.xml