通知中的播放/暂停按钮图像,安卓

15

我已经实现了音乐播放器,当流媒体音频正在播放时,它会触发自定义通知。

一切都正常工作,我可以使用通知中的按钮播放/暂停音频。唯一的问题是图像按钮:它无法在按钮点击时更改图像以指示播放/暂停。

在RemoteReceiver中使用remoteViews.setImageViewResource()不起作用。控件是使用BroadcastReceiver完成的,这是从播放器活动中触发通知的代码:

  public void setNotification(String songName){
      String ns = Context.NOTIFICATION_SERVICE;
      NotificationManager notificationManager = (NotificationManager) getSystemService(ns);

      @SuppressWarnings("deprecation")
      Notification notification = new Notification(R.drawable.ic_launcher, null, System.currentTimeMillis());

      RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.notification_view);
      notificationView.setImageViewResource(R.id.button1, R.drawable.pause);
      notificationView.setTextViewText(R.id.textView1, songName);

      //the intent that is started when the notification is clicked (works)
      Intent notificationIntent = new Intent(this, PlayerActivity.class);
      PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

      notification.contentView = notificationView;
      notification.contentIntent = pendingNotificationIntent;     

      Intent switchIntent = new Intent("com.example.test.ACTION_PLAY");
      PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, PendingIntent.FLAG_UPDATE_CURRENT);

      notificationView.setOnClickPendingIntent(R.id.play_pause, pendingSwitchIntent);
      notificationManager.notify(1, notification);
  }

通知的 notification.xml 内容为

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    <ImageView
        android:layout_alignParentLeft="true"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/ic_launcher"
        android:id="@+id/imgAppIc" />



    <TextView
        android:layout_toRightOf="@id/imgAppIc"
        android:singleLine="true"
        android:id="@+id/textView1"
        android:ellipsize="marquee"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="10dp"
        android:marqueeRepeatLimit ="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:scrollHorizontally="true"
        android:layout_width="170dp"
        android:layout_height="wrap_content"/>


    <ImageButton
        android:id="@+id/play_pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/play"

         />

</RelativeLayout>

这是RemoteReceiver类

public class RemoteControlReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.notification_view);    

        if(action.equalsIgnoreCase("com.example.test.ACTION_PLAY")){
            if(mediaplayer.isPlaying()){
                mediaplayer.pause();

                remoteViews.setImageViewResource(R.id.button1, R.drawable.play);
            }
            else {
                mediaplayer.start();
                remoteViews.setImageViewResource(R.id.button1, R.drawable.pause);
            }
        }
    }
}

最后,清单文件是:

 <receiver android:name=".RemoteControlReceiver">
            <intent-filter>
                <action android:name="com.Music.app.ACTION_PLAY" />
            </intent-filter>


        </receiver>

<activity android:name="PlayerActivity" />
4个回答

6

当你修改了remoteView后,你需要将notification.contentView设置为新的remoteView,这样它才能更新通知视图本身。

也就是说,在接收到BroadcastReceiver中的操作后,使用所需的按钮显示(暂停或播放)重新构建你的通知。

希望对你有所帮助。


那样做,通知不会闪烁吗? - An-droid
Twinkle,你能否在这里发布答案(带代码)? 我们已经尝试了好几天了... 在设置notification.contentView = new remoteView()之后, 通知按钮仍然无法工作。 - Omkar Jadhav
@OmkarJadhav,按照AlAsiri的建议,已经解决了问题,更改按钮图像后,重新通知notificationManager.notify(1, notification);即可显示更新。确保使用相同的ID,这里是数字1。希望你现在明白了,如果需要帮助,请告诉我。 - Twinkle
但这不会是一个新的通知吗? - suku
@suku 使用相同的ID以避免此问题。 - Mina F. Beshay
不是我的问题:从包com.nikola.starter发布的通知有问题:无法展开StatusBarNotification(pkg=com.nikola.starter user=UserHandle{0} id=1 tag=null score=20: Notification(pri=2 contentView=com.nikola.starter/0x7f040025 vibrate=default sound=default defaults=0xffffffff flags=0x1 kind=[null]))。 - Nikola Lukic

4

这应该可以解决问题。只需要将你的notificationnotificationManager设置为静态全局变量。

public class RemoteControlReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {  

        if(action.equalsIgnoreCase("com.example.test.ACTION_PLAY")){
            if(mediaplayer.isPlaying()){
                mediaplayer.pause();
                notificationView.setImageViewResource(R.id.button1, R.drawable.play);
                notification.contentView = notificationView;
                notificationManager.notify(1, notification);
            }
            else {
                mediaplayer.start();
                notificationView.setImageViewResource(R.id.button1, R.drawable.pause);
                notification.contentView = notificationView;
                notificationManager.notify(1, notification);
            }
        }
    }
}

这里有些奇怪。我有同样的代码但是不起作用。你怎么能像那样访问notificationView?Service1.mNotificationManager.notify(1, Service1.mBuilder.build()); 这行代码会导致应用程序崩溃... - Nikola Lukic

-1

大家好,你只需要在onReceiver方法中更新“notificationManager.notify(1, notification);”即可,在对同一通知进行更改后。将public static分配给您的通知和notificationManager,并在您的BroadcastReceiver扩展类中使用。

请参阅下面的代码以了解详细信息:


 public void onReceive(Context context, Intent intent) {


    String action1 = intent.getStringExtra("action1");
       // String action2 = intent.getStringExtra("action2");

        Log.i("Intent Received","Yes");


    if(say)
    {
        if (action1.equals("PauseAction"))
        {
            performAction1();
            say = false;

        }
    }

    else if(!say)
    {

        if (action1.equals("PauseAction"))
        {
            performAction2();
            say = true;


        }
    }

   public void performAction1()
    {

   if (mediaPlayer!=null)
 {

     if(mediaPlayer.isPlaying())
        {
            playButtonImage.setImageResource(R.drawable.playbutton);
            playButtonImage.setContentDescription("Play Button ");


 notificationView.setImageViewResource(R.id.remoteViewImageView,R.drawable.playbutton);
            mediaPlayer.pause();
            play = true;

            MainActivity.notificationManager.notify(1, notification);


        }

        Log.i("performAction1 called ","Yes");

    }
  }


    public void performAction2()
    {

        if (mediaPlayer != null)
        {
            mediaPlayer.start();
            playButtonImage.setImageResource(R.drawable.pause);
            playButtonImage.setContentDescription("Pause Button");
            notificationView.setImageViewResource(R.id.remoteViewImageView,R.drawable.pause);
            MainActivity.notificationManager.notify(1, notification);

            play = false;

            Log.i("performAction2 called ", "Yes");
        }
    }
}


-2
为什么不使用ImageView而不是ImageButton?使用ImageView,您可以通过removeViews.setImageViewResource(R.id.button1, R.drawable.pause);轻松更改其外观。

这个我没想到过。。我以前从来没试过这个,也许下次可以试试看。 - Twinkle
@Jason,无论是ImageView还是ImageButton都没关系。setImageViewResource()对它们两个都起到同样的作用。 - Tushar Monirul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接