2016-02-15 2 views
2

Это мое уведомление, мне нужно событие при нажатии кнопки при уведомлении. setOnClickPendingIntent не работает для меня. R.layout.mynotification находится под кодом. (Я не хочу addAction).Нажатие кнопки пользовательского уведомления

 Intent intent = new Intent(Fragmentz.ctx, NotificationReceiverActivity.class); 
     PendingIntent pIntent = PendingIntent.getActivity(Fragmentz.ctx, (int) System.currentTimeMillis(), intent, 0); 

     // Build notification 
     RemoteViews notificationView = new RemoteViews(Fragmentz.ctx.getPackageName(), 
       R.layout.mynotification); 

     Notification noti = new Notification.Builder(Fragmentz.ctx) 
       .setContentTitle("Radio") 
       .setContentText("").setSmallIcon(R.drawable.logo_notif) 
       .setContentIntent(pIntent) 
       .build(); 


     noti.contentView = notificationView; 
     NotificationManager notificationManager = (NotificationManager) Fragmentz.ctx.getSystemService(NOTIFICATION_SERVICE); 
     // hide the notification after its selected 
     noti.flags |= Notification.FLAG_AUTO_CANCEL; 

     notificationManager.notify(0, noti); 

R.layout.mynotification.xml

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:orientation="horizontal" 
    android:weightSum="100" > 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:scaleType="fitCenter" 
     android:text="Play" 
     android:id="@+id/not_button" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:adjustViewBounds="true" 
     android:src="@drawable/img_btn_play" 
     android:background="@null" 
     /> 


    </LinearLayout> 
+0

Смотрите, если мой ответ работает для вас. – Rohit5k2

ответ

0

Вы должны добавить намерение через pendingIntent так:

Intent intent = new Intent(context, desiredClass.class); 

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

mBuilder.setContentIntent(pendingIntent); 

Обратите внимание на FLAG_UPDATE_CURRENT флаг и проверить, если он является правильным для вашего дела.

+0

Прошу прощения, это не работает. В моем коде я уже написал это. Мне нужно событие нажатия кнопки. android: id = "@ + id/not_button" –

0

Это работает для меня:

// Creates an explicit intent for an ResultActivity to receive. 
    Intent resultIntent = new Intent(Fragmentz.ctx, NotificationReceiverActivity.class); 

    // This ensures that the back button follows the recommended convention for the back key. 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext); 
    stackBuilder.addNextIntent(resultIntent); 
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
+0

Мне не нужно событие кнопки с обратной связью. У меня есть моя кнопка на моем макете уведомлений. Мне нужно дать мероприятие для этого. –

0

Это должно работать для вас

Добавьте эту строку

Intent listener = new Intent(ctx, NotificationClickHandler.class); 
PendingIntent pListener = PendingIntent.getActivity(ctx, 0, listener, 0); 
notificationView.setOnClickPendingIntent(R.id.not_button, pListener); 

И это в манифесте

<receiver android:name="com.xxx.NotificationClickHandler" /> // Use your package name 

Наконец создать класс NotificationClickHandler

public class NotificationClickHandler extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //TODO: do your desired work here 
    } 
} 

Примечание: Вместо Activity это может быть сделано с помощью BroadcastReceiver тоже.

+0

Я пробовал, но он также не работает. :( –

+0

Так что щелчок не работает или вы получаете какую-либо ошибку? – Rohit5k2

+0

Щелчок не работает, и я не получил никаких ошибок, я пробовал как активность, так и BroadcastReceiver –

0

Что об этом

 RemoteViews contentView = new RemoteViews(c 
      .getPackageName(), R.layout.notification_custom); 
contentView.setTextViewText(R.id.buttonNotificationAccept, "Ok"); 
    contentView.setOnClickPendingIntent(R.id.buttonNotificationAccept, 
       resultPendingIntent); 
Смежные вопросы