按钮单击通知的侦听器

时间:2018-03-01 09:57:04

标签: android notifications clicklistener custom-notificaiton

我想在我正在创建的此通知的按钮点击上指定不同的操作。例如暂停和下一首或上一首歌曲。我希望歌曲的文本在通知面板中。但它现在没有显示出来。我尝试了传统方式,但没有奏效。

public void notifications() {
        try {

            CharSequence name = getString(R.string.app_name);
            int importance = NotificationManager.IMPORTANCE_HIGH;
            Intent notifyIntent = new Intent(this,AndroidBuildingMusicPlayerActivity.class);
            notifyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            Intent notificationIntent = new Intent(this, AndroidBuildingMusicPlayerActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

            packageName = getPackageName();
            // Setting our custom appearance for the notification
            notificationView = new RemoteViews(packageName, R.layout.notification);
            notificationView.setImageViewResource(R.id.notification_button_play, R.drawable.pause);
            notificationView.setImageViewResource(R.id.notification_button_skip, R.drawable.skip);
            notificationView.setTextViewText(R.id.notification_text_title,songTitle);
            notificationView.setImageViewResource(R.id.notification_button_back, R.drawable.back);
            Log.e("notification","title " + songTitle);
            notificationBuilder = new Notification.Builder(this);
            notificationBuilder.setContentIntent(pendingIntent)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setTicker("Music: Playing " + songTitle)

                    .setOngoing(true)
                    .setContentTitle("Music Player")

                    .setContentText(songTitle)
                    .setContent(notificationView);


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                notificationBuilder.setChannelId(mChannelId).build();
                mChannel = new NotificationChannel(mChannelId, name, importance);
            }

            Notification notification = notificationBuilder.build();


            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                manager.createNotificationChannel(mChannel);
            }
            manager.notify(0, notificationBuilder.build());
            //Toast.makeText(this, "DONE", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Log.e("Error notification","e = "+e.toString());
        }
    }

XML的代码如下:我附加了通知布局的输出:The blue player is mine

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/notification"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/notification_button_back"
        android:layout_width="53dp"
        android:layout_height="match_parent"
        android:contentDescription="@string/notification_button_skip"
        android:src="@drawable/back" />

    <ImageButton
        android:id="@+id/notification_button_play"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:contentDescription="@string/notification_button_play"
        android:src="@drawable/play" />

    <ImageButton
        android:id="@+id/notification_button_skip"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:contentDescription="@string/notification_button_skip"
        android:src="@drawable/skip" />

    <TextView
            android:id="@+id/notification_text_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:lines="1"
            android:singleLine="true"
            android:text="placeholder text"
            android:textStyle="bold" />    

0 个答案:

没有答案