2016-05-21 20 views
1

このコードはoverridesonNewIntent()アクティビティの方法です。この方法では、通知をクリックした結果、ListViewの項目を選択してに設定されました。問題はそれです; 2つの通知がある場合、最初の通知の必要な項目をクリックすると強調表示されますが、2番目の通知の前の項目をクリックすると再び強調表示されます。だから助けてください。必須ListViewの項目が選択されていません

コード:

@Override 
    protected void onNewIntent(Intent new_notification) { 

     caller = new_notification.getExtras().getString("CALLER"); 
     if(caller.equals("GenNot")) { 
      int myScrollTo = new_notification.getExtras().getInt("ID"); 
      Log.e("SEE NOW", "myScrollTo # "+myScrollTo, new Exception()); 
      remindersList.requestFocusFromTouch(); 
      remindersList.setSelection(myScrollTo); 
     } 
    } 

Notiicationコード:

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.support.v4.app.NotificationCompat; 



public class GenerateNotification { 

    public static void reminderNotification(Context context, int notification_id, String document_id, String name, String date, Location location) { 

     Intent intent = new Intent(context, ViewReminders.class); 
     intent.putExtra("CALLER","GenNot"); 
     intent.putExtra("ID", notification_id); 
     PendingIntent pIntent = PendingIntent.getActivity(context, notification_id, intent, 0); 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); 
     mBuilder.setTicker("Smart Locator"); 
     mBuilder.setSmallIcon(R.drawable.notification_icon); 
     mBuilder.setContentTitle(name); 
     DetailsContainer dc = new LocationDetails(context).getDetails(location); 
     mBuilder.setContentText(date + ", " + dc.area + " " + dc.locality); 
     mBuilder.setContentIntent(pIntent).getNotification(); 
     mBuilder.setAutoCancel(true); 
     mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL; 
     Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     mBuilder.setSound(alarmSound); 
     NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(document_id, notification_id, mBuilder.build()); 
    } 

} 

答えて

1

私は私のプロジェクトで同じ問題に直面してきました。受け取った後pendingIntent.cancel()に電話して解決しました。

A PendingIntent自体は単純です:

は、私はまた official documentationで良い説明がある notification_id

private final HashMap<String, PendingIntent> mIntents = new HashMap<String, PendingIntent>(); 
... 
// on post 
mIntents.put(challenge.getId(), pendingIntent); 
... 
// on click 
if (mIntents.containsKey(challengeId)) { 
    mIntents.remove(challengeId).cancel(); 
} 

を使用してそれを見つけた後、Mapに通知を投稿する前にPendingIntentへのリンクを保存し、それを検索するために使用された元のデータを記述しているシステムによって保持されているトークンへの参照。この は、所有アプリケーションのプロセスが終了したとしても、 PendingIntent自体は、 が与えられた他のプロセスから引き続き使用可能であることを意味します。作成中のアプリケーションが後で同一のトークンを表す PendingIntent(同じ操作、同じインテントアクション、データ、 カテゴリ、同じフラグ)を再び取得すると、有効な場合は PendingIntentが返されます はcancel()を呼び出して削除することができます。

この動作のために、2つのインテントが であると認識されることが、PendingIntentを取得する目的で同じであると考えられることが重要です。 一般的な間違いは、複数のPendingIntent オブジェクトを作成し、それぞれの "余分な"内容が異なるインテントのみを作成することです。 毎回異なるPendingIntentを取得することを期待しています。これは が発生しません。照合に使用されるインテントの部分は、Intent.filterEqualsで定義されたものと同じものが です。 Intent.filterEqualsと同等の2つのIntent オブジェクトを使用すると、 は両方とも同じPendingIntentを取得します。

これに対処する典型的な方法が2つあります。

あなたが本当に複数の異なるPendingIntentを必要とする場合は、 で(例えば、両方同時に を示している2つの通知として使用するように)同じ時間にアクティブなオブジェクト、あなたは違う何か があることを確認する必要があります。それらを異なる PendingIntentsに関連付けるように指示します。これは、 インテントによって考慮されるインテント属性のいずれかです。getEctivity(Context、int、Intent、int)、getActivities(Context、int、 Intent []、int)、getBroadcast(Context、int、Intent、int)または に提供された別のリクエストコード整数。コンテキスト、int、intent、int)

あなただけが使用する テントのいずれかの時間でアクティブなPendingIntentを必要とする場合は、代わりにキャンセルや意図に関連付けられている現在のどんなPendingIntent を変更するのいずれかのフラグ FLAG_CANCEL_CURRENTまたはFLAG_UPDATE_CURRENTを使用することができます供給は です。

関連する問題