【问题标题】:Binding Service doesn't call onBind and onServiceConnected绑定服务不调用 onBind 和 onServiceConnected
【发布时间】:2014-05-10 11:58:44
【问题描述】:

所以我正在构建我的 MusicPlayer,因此我创建了一个服务来管理所有内容。我打电话给ContextWrapper.bindService() 这应该叫onServiceConnectedonBind 对吧?但这对我来说不是这样。

如果我有什么问题,请告诉我。

这是我的代码:

这是我将 Activity 绑定到服务 (MusicUtils.java) 的方法。在我的活动中,它看起来像这样: MusicUtils.bindToService(this, this);

public static final ServiceToken bindToService(final Context context,
                                               final ServiceConnection callback) {
    Activity realActivity = ((Activity)context).getParent();
    if (realActivity == null) {
        realActivity = (Activity)context;
    }
    final ContextWrapper contextWrapper = new ContextWrapper(realActivity);
    contextWrapper.startService(new Intent(contextWrapper, MediaPlayerService.class));
    final ServiceBinder binder = new ServiceBinder(callback);
    Intent intent = new Intent().setClass(contextWrapper, MediaPlayerService.class);
    if (contextWrapper.bindService(intent, binder, Context.BIND_AUTO_CREATE)) {
        mConnectionMap.put(contextWrapper, binder);
        Log.e("MusicUtils","Bound");
        return new ServiceToken(contextWrapper);
    }
    return null;
}

这是我的 ServiceBinder:

public static final class ServiceBinder implements ServiceConnection {

    private final ServiceConnection mCallback;
    public ServiceBinder(final ServiceConnection callback) {
        mCallback = callback;
    }

    @Override
    public void onServiceConnected(final ComponentName className, final IBinder service) {
        mService = IMusicSlideService.Stub.asInterface(service);
        if (mCallback != null) {
            mCallback.onServiceConnected(className, service);
        }
    }

    @Override
    public void onServiceDisconnected(final ComponentName className) {
        if (mCallback != null) {
            mCallback.onServiceDisconnected(className);
        }
        mService = null;
    }
}

这是我的 onBind(MediaPlayerService.java) :

@Override
public IBinder onBind(Intent intent) {
    Log.e("Service", "onBind");
    return mBinder;
}

如果你需要什么,请告诉我!

谢谢!!!

【问题讨论】:

  • 我不知道你在用realActivity 和那些对ContextWrapper 的引用做什么。这是一个示例应用程序,演示了bindService() 的使用:github.com/commonsguy/cw-omnibus/tree/master/AdvServices/…
  • 我在另一个应用程序中看到了这个,所以我从那里拿走了它。不幸的是,即使您的实施,它仍然无法正常工作。 bindSerivce被调用时,onServiceConnectedonBind被调用对吗?
  • 我的实现有效。运行应用程序并在onBind()onServiceConnected() 中放置断点。两者都会被调用。通常,如果没有问题(例如,清单中缺少 <service> 元素),它们将被调用,并且问题应在 LogCat 中显示为警告或错误。
  • 毫无疑问,您的代码可以正常工作,但我的应用程序中肯定存在这样的问题。现在我知道onBind 被调用但onServiceConnected 没有。我在清单中得到了<service>。我真的不知道出了什么问题。

标签: java android service media-player


【解决方案1】:

我使用下面的代码,一切正常。

Intent intent = new Intent(mContext, Connector.class);
mContext.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);

您的问题可能在于使用 ContextWrapper 吗?

或者尝试改变 new Intent().setClass(contextWrapper, MediaPlayerService.class); to new Intent(contextWrapper, MediaPlayerService.class);

【讨论】:

  • 您不需要startService() 来绑定服务。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-05
  • 1970-01-01
  • 1970-01-01
  • 2020-10-23
  • 1970-01-01
相关资源
最近更新 更多