6

我正在尝试绑定服务,但onBind()总是返回 false。

这是ServiceConnection-

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        // This is called when the connection with our service has been established, 
        // giving us the service object we can use to interact with our service.
        mBoundService = ((ScheduleService.ServiceBinder) service).getService();
    }

    public void onServiceDisconnected(ComponentName className) {
        mBoundService = null;
    }
};

这是呼吁bindService()-

boolean test = getApplicationContext().bindService(new Intent(this, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);

这是 Manifest 中服务的声明——

<service android:name=".Notifications.ScheduleService" android:enabled="true"/>

我已经阅读了有关该主题的先前问题,但找不到答案(尝试使用 Application 上下文切换 Activity 上下文,但没有帮助)。

我正在使用 Fragments 和 ActionBarSherlock,并且我的 Activity 扩展SlidingFragmentActivity(这就是我使用应用程序上下文的原因,这没有帮助)。

编辑-这是我要启动的服务的代码-

public class ScheduleService extends Service {

    /**
     * Class for clients to access
     */
    public class ServiceBinder extends Binder {
        public ScheduleService getService() {
            return ScheduleService.this;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("ScheduleService", "Received start id " + startId + ": " + intent);

        // We want this service to continue running until it is explicitly stopped, so return sticky.
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    // This is the object that receives interactions from clients. See
    private final IBinder mBinder = new ServiceBinder();

    /**
     * Show an alarm for a certain date when the alarm is called it will pop up a notification
     */
    public void setAlarm(Calendar c) {
        // This starts a new thread to set the alarm
        // You want to push off your tasks onto a new thread to free up the UI to carry on responding
        new AlarmTask(this, c).run();
    }
}

任何帮助将不胜感激 。谢谢。

4

3 回答 3

1

为什么要使用应用上下文来绑定服务?bindService 方法是通过 ContextWrapper 调用的。这可能不是问题,但我会在绑定服务的地方和有连接的地方共享上下文。

在你的情况下,而不是

boolean test = getApplicationContext().bindService(new Intent(this, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);

我会做以下

boolean test = bindService(new Intent(this, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);

或者,如果您想在应用程序中保留全局上下文,请将所有内容移动到您的应用程序文件并以与上面建议的相同方式类似地调用它。

问题也可能出在您的应用程序包名称和清单中的服务声明上。如果您不确定,请确保在清单中为您的服务提供全局路由。

于 2013-03-24T14:08:14.513 回答
1

What is the fully qualified class-name of ScheduleService (i.e. including the full package-name)?

I'm asking this, because in your AndroidManifest.xml file, your service's name is .Notifications.ScheduleService, which seems a bit odd:

This tells me that either

  • The (last part of the) package-name contains a upper-case character... not so good.
    I would expect .notifications.ScheduleService instead, if this is the case.
  • The ScheduleService is defined within a file called Notifications.java. I would expect .Notifications$ScheduleService instead, if this is the case (dollar sign instead of period).
于 2013-03-19T22:27:11.727 回答
1

你的意思是bindService()返回false? onBind()返回IBinder类型。

请记住,服务绑定需要一些时间。如果您想在绑定完成后执行一些操作,您可以在onServiceConnected()方法中执行它。

public void onServiceConnected(ComponentName className, IBinder service) {

    mBoundService = ((ScheduleService.ServiceBinder) service).getService();
    Calendar c = new Calendar();
    mBoundService.setAlarm(c);
}

如果您需要这方面的更多指导,您需要向我们展示您的活动代码。

于 2013-03-21T06:56:02.353 回答