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 に答える
-2

ImageButton の代わりに ImageView を使用しないのはなぜですか? ImageView を使用すると、外観を簡単に変更できます。removeViews.setImageViewResource(R.id.button1, R.drawable.pause);

于 2014-12-23T08:14:17.173 に答える