2013-02-27 57 views
0

因此,我創建了一項服務,效果很好。它所做的只是計數。我有一個自定義類來處理使用通知的麻煩。這個類有一個getNotification,顯然它會返回它使用的通知。一切都很好,但我必須讓我的服務在前臺運行(它將一些重要的數據同步到應用程序中,直到它完成時才能中斷)現在,當我添加startForeground時,我添加了我的通知和一個ID像這樣離開它。 startForeground(1337,notification);在向服務添加startForeground()時,會創建兩個通知,而不是一個

問題我得到的是,我做的第一個notify()是出於某種原因,獨立於其他通知。所以當我運行它時會創建兩個通知。第一個卡住了第一次更新,其名稱爲「Zilean」,內容是「Counting」。另一個更新完美。我注意到,如果startForeground運行時ID爲0(startForeground(0,notification)),那麼這個問題就會得到解決,但是如果我終止了活動,通知就會消失。當ID爲<> 0.

我沒有這個問題太久,所以我使用一個虛擬服務,只有計數。我想知道,如果活動因用戶而死亡,或者只是因爲android決定刪除它的記憶,那麼該服務將繼續進行。

// onStartCommand 
@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    // NOTIFICATION SECTION 
    NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
      this); 

    notificationManagerClass = new SBNNotification(mNotifyManager, 
      mBuilder, true, 1, false, "Zilean", 
      "Initiating Counter", false); 
    notificationManagerClass.notificate(); 



     final Notification notification = notificationManagerClass.getNotification(); 
    notification.flags = flags; 
    notification.defaults = Notification.DEFAULT_LIGHTS; 

    startForeground(1337, notification); 


    new Timer().scheduleAtFixedRate(new TimerTask() { 
     int i=0; 
     @Override 
     public void run() { 

      notificationManagerClass.setContentTitle("Contando"); 
      notificationManagerClass.setContentText(String.valueOf(i)); 
      notificationManagerClass.notificate(); 
      i++; 
      Log.e("HOLAAA", String.valueOf(i)); 
     }}, 0, 1000); 

    return START_REDELIVER_INTENT; 
} 

那麼..我錯過了什麼?

回答

4

解決,我的錯誤是,通知ID wasnt一樣startForeground ID我經過(1337)

編輯:值得注意的是,通知ID和服務ID不能爲0,否則是很重要的它將與主要活動線程