从活动2(6.0.1 Android)返回活动1后,MediaBrowser.subscribe无法正常工作

时间:2017-04-02 15:04:45

标签: android

当我打开活动1(应用程序的主/启动器活动),包括MediaBrowser连接时,MediaBrowser.subscribe正常工作(onChildrenLoaded之后正在调用它),但当我打开其他一些活动时来自活动1的(数字2)(使用按钮点击监听器和意图),然后关闭此活动2,MediaBrowser.subscribe停止工作 - 订阅后未调用onChildrenLoaded,因此在活动2结束后{{1 SubscriptionCallback(活动1)& onConnected被调用但我的项目未更新,因为永远不会触发mMediaBrowser.subscribe(MEDIA_ID_ROOT, mSubscriptionCallback);

onChildrenLoaded

但它适用于4.4.4 Android(没有这样的问题)

更新

好像我在Google Bugs上发现了这个问题(某些开发者报告了这个问题):https://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=-has%3Asubcomponent%20-has%3Atarget%20emulator%20.&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened&groupby=&sort=&id=232455

但是没有解决方案

也有这样的错误:

private MediaBrowserCompat.ConnectionCallback mConnectionCallback =
            new MediaBrowserCompat.ConnectionCallback() {
                @Override
                public void onConnected() {
                    Log.i(TAG, "onConnected");
                    mMediaBrowser.unsubscribe(MEDIA_ID_ROOT, mSubscriptionCallback);
                    mMediaBrowser.subscribe(MEDIA_ID_ROOT, mSubscriptionCallback);

                }
            };

private MediaBrowserCompat.SubscriptionCallback mSubscriptionCallback = new MediaBrowserCompat.SubscriptionCallback() {

        @Override
        public void onChildrenLoaded(String parentId, List<MediaBrowserCompat.MediaItem> children) {
            Log.i(TAG, "onChildrenLoaded"); // isn't being called on android 6.0.1 after I got back to this activity from other
        if (children != null) {
            if (mMediaItems != null) {
                mMediaItems.clear();
                mMediaItems = null;
            }
            mMediaItems = children;
            if (mAdapter == null) {
                mAdapter = new Adapter();
                mRecyclerView.setAdapter(mAdapter);
            } else {
                mAdapter.notifyDataSetChanged();
            }
        }
        }

        @Override
        public void onError(String id) {
            Log.i(TAG, "SubscriptionCallback onError");
        }
    };

更新2

还有

https://github.com/googlesamples/android-UniversalMusicPlayer/issues/92

mb此链接的最后评论也将为我解决这个问题

更新3

是的,github.com/googlesamples/android-UniversalMusicPlayer/issues/92#issuecomment-287668132解决了这个问题:

  

将MediaBrowserCompat.connect()从onStart()移动到onCreate()和   将MediaBrowserCompat.disconnect()从onStop()移动到onDestroy()。它   现在有效。

3 个答案:

答案 0 :(得分:0)

  1. This issue was marked as fixed at google issuetracker in Support Lib v 25.4.0
  2. solution to

move MediaBrowserCompat.connect() from onStart() to onCreate(), and move MediaBrowserCompat.disconnect() from onStop() to onDestroy()

works for me.

  1. when I moved them and fixed the original issue, the next one appeared: MediaController Callback didn't work. It seems for me as caused by the similar bug. So I have moved call to unsubscribe() to onDestroy() too, and everything works now.

So my code now looks like:

protected void onCreate(Bundle savedInstanceState) {
    ...    
    mediaBrowser = new MediaBrowserCompat(this, new ComponentName(this, AudioService.class),
            connCallbacks,
            null);
    mediaBrowser.connect();
    ...
}

@Override
protected void onStart() {
    super.onStart();
}

@Override
protected void onStop() {
    super.onStop();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    MediaControllerCompat cntrlr = MediaControllerCompat.getMediaController(this);
    if(cntrlr != null) {
        cntrlr.unregisterCallback(cntrlrCallback);
    }
    if(mediaBrowser.isConnected()) {
        mediaBrowser.unsubscribe(mediaBrowser.getRoot());
        mediaBrowser.disconnect();
    }
}

答案 1 :(得分:0)

临时解决方案:

  

将MediaBrowserCompat.connect()从onStart()移动到onCreate()和   将MediaBrowserCompat.disconnect()从onStop()移动到onDestroy()。它   现在有效。

必须在v25.4.0中修复: https://issuetracker.google.com/issues/37133265#comment3

答案 2 :(得分:0)

参考https://github.com/processing/processing-android/issues/413

问题已修复并在 25.4.0版支持库 中发布。

如果问题仍然存在,请在Google issue tracker报告,然后重新审核。