2017-05-24 5 views
1

問題は一度通知が作成されたが、他の通知は作成されず、最後の通知を更新しない。 私は新しい通知に新しい通知を作成していないフォアグランドアンドロイドサービス

着信通知方法を取得したい:サービスから

private void NotificationLoop() { 

    // one notification 
     G.HANDLER.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       populateNotification(1, "task id : " + 1, 20); 
      } 
     }, 2000); 
// two notification 
     G.HANDLER.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       populateNotification(2, "task id : " + 2, 40); 
      } 
     }, 2000); 
// three notification 
     G.HANDLER.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       populateNotification(3, "task id : " + 3, 80); 
      } 
     }, 3000); 


    } 

populateNotification私の方法を:

private void populateNotification(int id, String title, int percent) { 

     Notification notification; 
     Intent CancelIntent = new Intent(this, ServiceDownload.class); 
     CancelIntent.setAction(Constant.ACTION.CANCEL_DOANLOAD_ACTION); 
     CancelIntent.putExtra("ID", id); 
     PendingIntent CCancelIntent = PendingIntent.getService(this, 0, CancelIntent, 0); 

     Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); 

     Intent notificationIntent = new Intent(this, MainActivity.class); 
     notificationIntent.setAction(Intent.ACTION_MAIN); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

     notification = new NotificationCompat.Builder(this) 
       .setContentTitle(title) 
       .setTicker(title) 
       .setContentText(title) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false)) 
       .setContentIntent(pendingIntent) 
       .setAutoCancel(false) 
       .setOngoing(false) 
       .addAction(android.R.drawable.ic_menu_close_clear_cancel, this.getResources().getString(R.string.Cancel), CCancelIntent) 
       .setProgress(100, percent, false) 
       .setStyle((new NotificationCompat.BigTextStyle().bigText(title))) 
       .build(); 
     startForeground(id, notification); 
    } 
+0

を起動します。 1つのサービスでstartForegroundに対して1つの通知しか使用できないようです。サービスでstartForegroundの代わりにNotificationManagerで2番目と3番目の通知を表示 –

+0

ありがとう、仕事 –

答えて

0

フォアグラウンドサービスは唯一つの通知があります。

startForeground(id, notification);に電話をかけたときに置き換えます。

あなたはあなたの問題を解決するために多くの選択肢を持っていけない

  1. 停止フォアグラウンドサービスにサービスを提供して再度起動し、その通知は 正確新しいことが、ありますに関係ない、新しい通知を作成します。新しい通知であり、古い通知が更新されていないことをユーザーは気付かないでしょう。
  2. 新しい通知で新しいフォアグラウンドサービスを開始します。
関連する問題