在通知播放/暂停按钮图像,机器人 [英] play/pause button image in notification ,Android

查看:186
本文介绍了在通知播放/暂停按钮图像,机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了音乐播放器,它会调用自定义通知时,流音频播放

I have implemented music player which fires custom notification when stream audio is playing

一切工作不错,我可以播放/暂停使用通知按钮的声音。 唯一的问题我已经是图像按钮,就不能更改图片点击按钮来指示播放/或暂停

everything is working good and I can play /pause the audio using the button in notification. the only problem I have is the image button , it can't change image on click the button to indicate play / or pause

在RemoteReciever使用remoteViews.setImageViewResource()什么也不做

using remoteViews.setImageViewResource() in RemoteReciever do nothing

在控制使用的BroadcastReceiver所做的是

the control is done using 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);
  }

的通知XML是

the notification xml is

<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类

this is the RemoteRecivier class

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" />

请,有利于解决这个问题,我已经坚持了很长一段时间 在此先感谢

please , help to solve this , I've been stuck for long time thanks in advance

推荐答案

您已经进行了修改后,你notification.contentView设置新的远程视窗,因此它可以更新通知视图本身。

You have to set your notification.contentView to the new remoteView after it was changed so it can update the notification view itself.

含义,您将收到的动作在你的BroadcastReceiver后,与所需的按钮显示(暂停或播放)

Meaning, after you receive the action in your BroadcastReceiver, re-build your notification with the desired button display ( pause or play )

希望这有助于

这篇关于在通知播放/暂停按钮图像,机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆