2011-07-10 13 views
0

我想在一天的特定時間發出通知。我有這個代碼,但我從來沒有收到通知。有任何想法嗎?Android:服務中的通知不是工作

*注意:整個代碼的一部分被刪除。我使用這些零件來更改小部件上的文本(我有一個應用程序小部件),並且使用了代碼(month_tod和day_tod)中聲明的相同變量,並且該代碼部分工作正常。

public final class UpdateService extends Service { 

    @Override 
    public void onStart(Intent intent, int startId) { 

     final Calendar today = Calendar.getInstance(); 
     int month_tod = today.get(Calendar.MONTH)+1; 
     int day_tod = today.get(Calendar.DAY_OF_MONTH); 
     int hour_tod = today.get(Calendar.HOUR); 
     int min_tod = today.get(Calendar.MINUTE); 

    if ((month_tod==7) && (day_tod==10) && (hour_tod==11) && (min_tod==29)) 
    { 

    NotificationManager mNotManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    String MyText = "Reminder"; 
    Notification mNotification = new Notification(
     R.drawable.icon,     
     MyText,       
     System.currentTimeMillis()  ); 

    String name; 
    name = "Demoname"; 
    String MyNotifyTitle ="Don’t forget"; 
    String MyNotifiyText = "Take your medicine"; 
    Intent MyIntent = new Intent(getApplicationContext(), MainActivity.class); 
    MyIntent.putExtra("extendedTitle", MyNotifyTitle); 
    MyIntent.putExtra("extendedText" , MyNotifiyText); 
    PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),0,MyIntent,PendingIntent.FLAG_CANCEL_CURRENT); 

    mNotification.setLatestEventInfo( getApplicationContext(), 
      MyNotifyTitle, 
      MyNotifiyText, 
      StartIntent); 
    int HELLO_ID = 1; 
    mNotManager.notify( HELLO_ID , mNotification); 
    } 
    } 

    public void onCreate(Bundle savedInstanceState) { 

    } 

    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
} 

回答

0

您的服務從哪裏開始?您需要使用AlarmManager才能以x秒/分鐘的速度啓動它。

+0

就像我說的,代碼的另一部分工作,所以我不認爲這是問題。順便說一下,它是從AppWidgetProvider的onUpdate方法開始的。 – erdomester