Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnBind() on service always returns False - Android

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

This is the code for the 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 call to bindService() -

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

This is the declaration of the service in the Manifest -

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

I've read previous questions regarding the subject and couldn't find an answer(tried switching the Activity context with the Application context, but it didn't help).

I'm using using Frgaments and ActionBarSherlock, and my Activity extends SlidingFragmentActivity (That's why i'm using the application context, which doesn't help).

Edit - This 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 will be appreciated . Thanks.

like image 284
Tofira Avatar asked Mar 14 '13 13:03

Tofira


1 Answers

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).
like image 89
Streets Of Boston Avatar answered Sep 28 '22 09:09

Streets Of Boston