无法从服务更新片段中的文本视图

Unable to update textview in Fragment from Service

我是 运行 运行音频的服务。我在应该显示正在播放的曲目的标题的片段布局中创建了一个 TextView

目前,我已将 TextView 设置为在用户从 Fragment 中选择曲目或单击跳到下一个或上一个按钮时更新:

   @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

//other stuff

        String[] topText = getResources().getStringArray(R.array.topText_array);
        songNowPlayingLabel.setText("Now Playing : " + topText[position]);
   }


    button_skipNext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    musicService.playNext();

                    songNowPlayingLabel.setText("Now Playing : " + topText[musicService.songPosn]);

                }
            });

但是,TextView 不会在当前曲目播放完后更新,服务会自动播放下一首曲目。此外,该应用程序还使用自定义通知,当应用程序不再显示在屏幕上但正在服务中播放音乐时,该通知将显示。在这种情况下,当按下 next/previous 的通知按钮时,TextView 不会更新。

我希望 TextView 在通过通知按钮更改曲目或完成曲目时进行更新。为此,我创建了一个广播侦听器并将其添加到 MusicService.javaonStartCommand 中。但是,这对 TextView.

没有影响

我在同一问题上浏览了其他一些线程,但无法解决此问题。我做错了什么?

xml 的 TextView

           <TextView
                android:id="@+id/songNowPlayingLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:textColor="#eeeeee"
                android:textStyle="bold"
                android:singleLine="true"
                android:ellipsize="marquee"
                android:marqueeRepeatLimit="marquee_forever"
                android:focusableInTouchMode="true"/>

MusicService.java

的相关部分
        static final public String BROADCAST_ACTION = "com.myapplication.broadcastSongNowPlayingLabel";

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {

            //... other stuff

            intent = new Intent(BROADCAST_ACTION);
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

Fragment.java

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

            // other stuff

            bManger = LocalBroadcastManager.getInstance(getActivity());
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(MusicService.BROADCAST_ACTION);
            bManger.registerReceiver(broadcastReceiver, intentFilter);

    }

        private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals(MusicService.BROADCAST_ACTION)) {

                String[] topText = getResources().getStringArray(R.array.topText_array);
                songNowPlayingLabel.setText("Now Playing : " + topText[musicService.songPosn]);
            }
        }
    };
    LocalBroadcastManager bManger;

问题与发送或接收广播无关,问题是即使在 Fragment 中接收到广播后,TextView 也没有更新。

您正在片段中使用 LocalBroadcastManager。

在MusicService.java下面使用:

LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);

改为

sendBroadcast(intent);

经过大量挖掘,这就是我想出的

在MusicService.java

@Override
    public void onCreate() {
        super.onCreate();

        songPosn = 0  // defined as a public integer
        //other stuff
        topText = getResources().getStringArray(R.array.topText_array);
        intent = new Intent(BROADCAST_ACTION);
    }

然后我进入我的 playSong() 方法。这是每次用户对音频进行操作时都会调用的方法,因此我认为它可以解决我的问题。

    public void playSong() {
        //other stuff    
        intent.putExtra("nowPlaying", topText[songPosn]);
        sendBroadcast(intent);
    }

然后在我的 fragment.java 文件中

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
         //other stuff
         broadcastReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    updateUI(intent);
                }
            };
    }

    private void updateUI(Intent intent){
        String nowPlaying = intent.getStringExtra("nowPlaying");
        songNowPlayingLabel.setText("Now Playing : " + nowPlaying);
    }