I want to make a custom notification with buttons, but I do not know how to make the system respond to pressing. More precisely, the service that will be responsible for playing music should respond to pressing. I have xml notifications:

<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_centerVertical="true" android:orientation="horizontal"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="7dp" android:layout_marginRight="7dp" android:src="@mipmap/ic_launcher" app:srcCompat="@mipmap/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="vertical"> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TestRadio" /> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Название песни" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentEnd="true" android:layout_centerVertical="true" android:gravity="center_vertical"> <ImageView android:id="@+id/imgPlayPause" android:layout_width="53dp" android:layout_height="53dp" android:src="@drawable/ic_pause" app:srcCompat="@drawable/ic_pause" /> <ImageView android:id="@+id/imgStop" android:layout_width="53dp" android:layout_height="53dp" android:src="@drawable/ic_stop" app:srcCompat="@drawable/ic_stop" /> </LinearLayout> 

ImageView c id @+id/imgPlayPause and @+id/imgStop act as buttons. The first button will change (play / stop) and the notification should not disappear. And the second will disable the player and delete the notification.

A notification is created in the service, like this:

  RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.notification_player); Intent intentPlayPause = new Intent(context, MediaPlayerService.class); remoteViews.setImageViewResource(R.id.imgPlayPause, R.drawable.ic_play_arrow); intentPlayPause.putExtra("button", "play"); PendingIntent piPlayPause = PendingIntent.getBroadcast(context, 0, intentPlayPause, PendingIntent.FLAG_CANCEL_CURRENT); remoteViews.setOnClickPendingIntent(R.id.imgPlayPause, piPlayPause); Intent intentStop = new Intent(context, MediaPlayerService.class); intentStop.putExtra(BUTTON_EXTRA_NAME, STOP_BUTTON_EXTRA); PendingIntent piStop = PendingIntent.getBroadcast(context, 0, intentStop, PendingIntent.FLAG_CANCEL_CURRENT); remoteViews.setOnClickPendingIntent(R.id.imgStop, piStop); Notification notif = new Notification.Builder(context) .setSmallIcon(R.mipmap.ic_launcher) .setContent(remoteViews) .setOngoing(true) .setPriority(NotificationCompat.PRIORITY_MAX) .build(); nManager.notify(NOTIFICATION_ID, notif); 

In the service I create BroadcastReceiver:

 @Override public void onCreate() { super.onCreate(); Log.d("radio", "Service Created"); nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); br = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { int button = intent.getIntExtra("button", -1); // здесь будет код, которые что-то делает } }; IntentFilter intentFilter = new IntentFilter(getPackageName()); registerReceiver(br, intentFilter); } 

As a result, nothing happened, nothing comes to the broadcastReceiver, how can I fix the code so that the idea may work?

    0