如何在android中添加通知按钮?

fra*_*ish 74 notifications android widget button

我的应用程序播放音乐,当用户通过从屏幕顶部(或平板电脑屏幕右下角的通用)打开通知屏幕时,我想向他们显示一个按钮,以停止当前播放的音乐并再次启动它他们要.

我不打算将小部件放在用户的主屏幕上,而只是放入通知中.我怎样才能做到这一点?

小智 6

您可以为该动作创建一个意图(在这种情况下,请停止播放),并将其作为动作按钮添加到通知中。

Intent snoozeIntent = new Intent(this, MyBroadcastReceiver.class);
snoozeIntent.setAction(ACTION_SNOOZE);
snoozeIntent.putExtra(EXTRA_NOTIFICATION_ID, 0);
PendingIntent snoozePendingIntent =
        PendingIntent.getBroadcast(this, 0, snoozeIntent, 0);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(pendingIntent)
        .addAction(R.drawable.ic_snooze, getString(R.string.snooze),
                snoozePendingIntent);
Run Code Online (Sandbox Code Playgroud)

请参考Android文档


Ank*_*pta 6

我将尝试提供我使用过的解决方案,大多数音乐播放器也使用相同的技术在通知栏中显示播放器控件。

我正在运行一个用于管理媒体播放器及其所有控件的服务。Activity 用户控件通过向服务发送 Intents 来与服务交互,例如

 Intent i = new Intent(MainActivity.this, MyRadioService.class);
        i.setAction(Constants.Player.ACTION_PAUSE);

        startService(i);
Run Code Online (Sandbox Code Playgroud)

为了在服务类中接收意图并执行操作,我在服务的 onStartCommand 方法中使用以下代码

 @Override
public int onStartCommand(Intent intent, int flags, int startId) {


    if (intent.getAction().equals(Constants.Player.ACTION_PAUSE)) {
        if(mediaPlayer.isPlaying()) {
            pauseAudio();
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在准确回答您的问题以显示带有播放控件的通知。您可以调用以下方法来显示带有控件的通知。

   //    showNotification
private void startAppInForeground() {
//  Start Service in Foreground

    // Using RemoteViews to bind custom layouts into Notification
    RemoteViews views = new RemoteViews(getPackageName(),
            R.layout.notification_status_bar);

// Define play control intent 
   Intent playIntent = new Intent(this, MyRadioService.class);
    playIntent.setAction(Constants.Player.ACTION_PLAY);

// Use the above play intent to set into PendingIntent
    PendingIntent pplayIntent = PendingIntent.getService(this, 0,
            playIntent, 0);

// binding play button from layout to pending play intent defined above 
views.setOnClickPendingIntent(R.id.status_bar_play, pplayIntent);
views.setImageViewResource(R.id.status_bar_play,
            R.drawable.status_bg);

Notification status = null;
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        status = new Notification.Builder(this).build();
    }

  status.flags = Notification.FLAG_ONGOING_EVENT;
    status.icon = R.mipmap.ic_launcher;
    status.contentIntent = pendingIntent;
    startForeground(Constants.FOREGROUND_SERVICE, status);
Run Code Online (Sandbox Code Playgroud)

希望这真的对你有帮助。你将能够实现你想要的。有一个快乐的编码:)


小智 6

添加操作按钮以进行通知

Intent snoozeIntent = new Intent(this, MyBroadcastReceiver.class);
snoozeIntent.setAction(ACTION_SNOOZE);
snoozeIntent.putExtra(EXTRA_NOTIFICATION_ID, 0);
PendingIntent snoozePendingIntent =
        PendingIntent.getBroadcast(this, 0, snoozeIntent, 0);

NotificationCompat.Builder mBuilder = new 
NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(pendingIntent)
        .addAction(R.drawable.ic_snooze, getString(R.string.snooze),
                snoozePendingIntent);
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请访问 https://developer.android.com/training/notify-user/build-notification.html


Cha*_*pta 5

    // It shows buttons on lock screen (notification).

Notification notification = new Notification.Builder(context)
    .setVisibility(Notification.VISIBILITY_PUBLIC)
    .setSmallIcon(R.drawable.NotIcon)
    .addAction(R.drawable.ic_prev, "button1",ButtonOneScreen) 
    .addAction(R.drawable.ic_pause, "button2", ButtonTwoScreen)  
   .....
    .setStyle(new Notification.MediaStyle()
    .setShowActionsInCompactView(1)
    .setMediaSession(mMediaSession.getSessionToken())
    .setContentTitle("your choice")
    .setContentText("Again your choice")
    .setLargeIcon(buttonIcon)
    .build();
Run Code Online (Sandbox Code Playgroud)

请参阅此以获得更多详细信息,请单击此处。