OnBind () on service always returns False - Android

I am trying to bind a service, but onBind() always returns false.

This is the code for 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; } }; 

This is a call to bindService() -

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

This is a service declaration in the manifest -

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

I read the previous questions on the topic and could not find the answer (I tried switching the context of the Activity with the application context, but this did not help).

I use Frgaments and ActionBarSherlock, and my Activity extends SlidingFragmentActivity (This is why I use an application context that does not help).

Edit is the code of the service I'm trying to start -

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

Any help would be appreciated. Thanks.

+6
source share
3 answers

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

I ask for this because in your AndroidManifest.xml file, your service name is .Notifications.ScheduleService , which seems a bit strange:

It tells me that either

  • The name (last part) of the package contains an uppercase character ... not very good.
    I would expect .Notifications.ScheduleService if so.
  • ScheduleService defined in a file called Notifications.java. I would expect .Notifications$ScheduleService instead, if so (dollar sign instead of period).
+1
source

Do you mean bindService() returns false? onBind() returns an IBinder type.

Keep in mind that binding to a service takes some time. If you want to perform an action after the completion of the binding, you can perform it in the onServiceConnected() method.

 public void onServiceConnected(ComponentName className, IBinder service) { mBoundService = ((ScheduleService.ServiceBinder) service).getService(); Calendar c = new Calendar(); mBoundService.setAlarm(c); } 

If you need more recommendations about this, you need to show us your operation code.

+1
source

Why are you using the application context to bind a service? The bindService method is called through ContextWrapper. This may not be a problem, but I would share the contexts where you connect the service and where you have the connection.

In your case, instead of

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

I would do the following

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

Or, if you want to keep the global context in the application, move everything to your application file and name it in the same way as suggested above.

The problem may also be the package name of your application and the declaration of your service in your manifest. If you are not sure that you want to provide a global route to your service in the manifest.

0
source

All Articles