6

我试图绑定一个服务,但是onBind()总是返回false。OnBind()on service always always returns False - Android

这是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); 

这是清单的服务宣言 -

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

我已阅读了有关该主题的以前的问题,但无法找到答案wer(尝试用Application上下文切换Activity上下文,但它没有帮助)。我使用的是Frgaments和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(); 
    } 
} 

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

+0

我想你应该删除Manifest中的.Notifications。 – 2013-03-14 17:09:50

+0

发布您的服务实施代码。 – yorkw 2013-03-17 20:11:23

+0

我用这个实现编辑了这个问题。 – Tofira 2013-03-18 21:55:02

回答

1

什么是ScheduleService的全限定类名(即包含完整的包名)?

我问这个,因为在你的AndroidManifest.xml文件,你的服务的名称为.Notifications.ScheduleService,这似乎有点奇怪:

这告诉我,要么

  • (的最后一部分)包名包含一个大写 字符...不太好。
    相反,如果是这种情况,我会期待.notifications.ScheduleService
  • ScheduleService在名为Notifications.java的文件中定义。 相反,如果是这种情况(美元符号而不是期间),我会期待.Notifications$ScheduleService
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); 
} 

如果您需要更多指导,请告诉我们您的活动代码。

0

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

在你的情况,而不是

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); 

或者,如果你想保持应用程序中的全球背景下,将一切,你的应用程序文件和呼叫它与上面提出的方式类似。

该问题也可能出现在您的应用的软件包名称和您的清单中的服务声明中。如果您不确定,请确保在清单中提供通往您的服务的全球路线。