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>

这是 RemoteRecivier 类

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

4 回答 4

6

您必须在更改后将 notification.contentView 设置为新的 remoteView,以便它可以更新通知视图本身。

这意味着,在您在 BroadcastReceiver 中收到操作后,使用所需的按钮显示(暂停或播放)重新构建通知

希望这可以帮助

于 2014-06-29T05:45:34.633 回答
4

这应该有效。只需创建notification一个notificationManager静态全局变量即可。

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);
            }
        }
    }
}
于 2015-03-17T14:10:10.023 回答
-1

嘿伙计们,您只需要更新“notificationManager.notify(1, notification);” 在您对同一通知进行更改后,在您的 onReceiver 方法中。将公共静态分配给您的 notification 和 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");
        }
    }
}

于 2019-09-09T07:13:39.120 回答
-2

为什么不使用 ImageView 而不是 ImageButton?使用 ImageView,您可以简单地更改其外观removeViews.setImageViewResource(R.id.button1, R.drawable.pause);

于 2014-12-23T08:14:17.173 回答