2016-05-21 11 views
1

通知がクリックされたときにListItemをハイライトしたいと思います。 ListViewと表示されている私の活動は既に開いているので、通知をクリックすると再び開くことができません。私は多くを検索しましたが、私は通知のための方法はないと思っています。onClick()だからどうすればいいのか教えてください。通知のためのonClick()リスナー

これは、私はあなたがあなたの保留中の意図に余分を置くことができ、通知

Intent intent = new Intent(); 
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()); 
+0

final Intent intent = new Intent(this, MyActivity.class); intent.setData(data); intent.putExtra("NotClick", true); final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent), 0); 

そして、あなたの活動に次のコードを使用しますどのアクティビティでそれを処理できますか。そこから、クリックした通知の種類と、選択したいListViewデータの位置を知るためのデータが必要です –

+0

[通知のためにクリックリスナーを設定する方法は?](http://stackoverflow.com/質問/ 7184963 /通知設定のための設定方法) –

答えて

3

を生成しています方法です。

final Intent intent = new Intent(this, MyActivity.class); 
intent.setData(data); 
intent.putExtra("key", "clicked"); 
final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent), 0); 

あなたのアクティビティが「再起動」されると、受け取ったバンドルから通知がクリックされたかどうかを確認できます。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    processIntent(getIntent()); 
} 

@Override 
protected void onNewIntent(Intent intent) {  
    processIntent(intent); 
}; 

private void processIntent(Intent intent){ 
    //get your extras 
    //if clicked, do something with ListView 
} 

this questionを確認してください。

+1

他の場所からの回答をコピーしないようにしてください –

+0

私は答えをコピーしていません。通知、私はコードのソースを配置しています。 –

+0

確かに。私はあなたがより多くの評判を得ると、重複として質問をマークすることができます。 –

0

保留中の意図に余分を置く:私は、通知をクリックすると、あなたがマニフェストに指定することができます意図を、作成考える

@Override 
protected void onNewIntent(Intent intent) { 
    try { 
     //on click notification 
     Bundle extras = intent.getExtras(); 
     if (extras.getBoolean("NotClick")) { 
      //Do your stuff here 
     } 
    } catch (Exception e) { 
     Log.e("onclick", "Exception onclick" + e); 
    } 
} 
関連する問題