簡體   English   中英

通知中播放/暫停按鈕圖片,Android

[英]play/pause button image in notification ,Android

我已經實現了音樂播放器,當播放流音頻時會觸發自定義通知。

一切正常,我可以使用通知中的按鈕播放/暫停音頻。 唯一的問題是圖像按鈕:單擊按鈕以指示播放/暫停不能更改圖像。

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

更改后,必須將notification.contentView設置為新的remoteView,以便它可以更新通知視圖本身。

意思是,在您的BroadcastReceiver中接收到動作之后,請使用所需的按鈕顯示(暫停或播放)重新構建通知

希望這可以幫助

這應該工作。 只需將您的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);
            }
        }
    }
}

大家好,您只需要更新“ notificationManager.notify(1,notification);” 對相同的通知進行更改后,請在onReceiver方法中。 將public static分配給您的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");
        }
    }
}

為什么不使用ImageView代替ImageButton? 使用ImageView,您可以簡單地使用removeViews.setImageViewResource(R.id.button1, R.drawable.pause);更改其外觀removeViews.setImageViewResource(R.id.button1, R.drawable.pause);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM