2016-04-15 9 views
3

私は一部のアプリケーションがリアルタイムで通知コンテンツを更新するのを見ました。 1つの例は、カウントダウンタイマーアプリケーションです。 + ADD 1 MINボタンをアンドロイド通知をリアルタイムで更新するにはどうすればよいですか?

enter image description here

クリックすると、すぐに遅滞なく通​​知テキストを変更します。これはどうですか?つまり、ユーザーがボタンをクリックしたときにリアルタイムで通知を更新するにはどうすればいいですか?

enter image description here

答えて

1

は、あなたがこれを見ましたか? android docsから...

3

私は通常、メンバー変数としてNotificationCompat.Builderを宣言しています。

次に、そのビルダーを使用して今後通知を更新できます。

private NotificationCompat.Builder mBuilder = null; 

public void refreshNotifications() { 
    if(mBuilder == null) { 
     mBuilder = new NotificationCompat.Builder(context); 
     mBuilder.setAutoCancel(false); 
     mBuilder.setOngoing(true); 
     mBuilder.setOnlyAlertOnce(true); 
     mBuilder.setVisibility(Notification.VISIBILITY_PRIVATE); 
     mBuilder.setSmallIcon(R.drawable.ic_launcher_notification_outline); 
     mBuilder.setOngoing(true); 
     mBuilder.setContentTitle(context.getString(R.string.downloading_file)); 
    } 

    mBuilder.setContentText(context.getString(R.string.total_progress, percentProgress)); 
    notificationManager.notify(DOWNLOADING_FILE, mBuilder.build()); 
} 
-1

テキストビューでタイマーを設定します。 "+ ADD 1分"が押されるたびに。古いタイマー+ 1分でタイマーを更新してください。 @Assafは、あなたがこの文書を読むことができるカスタムレイアウトを行うために

public void refreshNotifications(String message) { 
    NotificationManager mNotificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    // Sets an ID for the notification, so it can be updated 
    int notifyID = 1; 
    mBuilder = new NotificationCompat.Builder(this) 
      .setContentTitle("New Message") 
      .setContentText("You've received new messages.") 
      .setSmallIcon(R.mipmap.ic_launcher); 
    int numMessages = 0; 
    // Start of a loop that processes data and then notifies the user 
    mBuilder.setContentText(message) 
      .setNumber(++numMessages); 
    // Because the ID remains unchanged, the existing notification is 
    // updated. 
    mNotificationManager.notify(
      notifyID, 
      mBuilder.build()); 
} 

そして、前に言ったようにテストするには