Search code examples
javaandroidserviceandroid-serviceandroid-service-binding

Simple service won't bind : bindService return false, service connection never triggered


This is my first time using Service in Android and my service won't bind.

The Service code :

package mackosoft.almightymessage;
public final class MainService extends Service {
    private final static String TAG = "Main Service";
    private final IBinder binder = new MainServiceBinder();

    @Override
    public final void onCreate() {
        super.onCreate();
        Log.d(TAG, "Service created");
        Toast.makeText(this, "Service created", Toast.LENGTH_SHORT).show();
    }

    @Override
    public final int onStartCommand(final Intent intent, final int flags, final int startId) {
        return START_STICKY;
    }


    @Override
    public final void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "Service created");
        Toast.makeText(this, "Service destroyed", Toast.LENGTH_SHORT).show();
    }


    @Override
    public final IBinder onBind(final Intent intent) {
        Toast.makeText(this, "Service binded", Toast.LENGTH_SHORT).show();
        return this.binder;
    }

public final class MainServiceBinder extends Binder {
        final MainService getService() {
            return MainService.this;
        }
    }
}

Manifest file :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="mackosoft.almightymessage" >

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_logo"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|keyboardHidden"
        android:icon="@drawable/ic_logo"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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

</Application
</manifest>

And finally the part in MainActivity where I try to bind the service :

public final class MainActivity extends AppCompatActivity {
@Override
protected final void onStart() {
        super.onStart();
        final Intent intent = new Intent(MainActivity.this, MainService.MainServiceBinder.class);
        final boolean status = bindService(intent, this.connection, Context.BIND_AUTO_CREATE);
        Log.d(TAG, "Service binded ? " + status);
    }

 private final ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.d(TAG, "Service connected !");
            service = ((MainService.MainServiceBinder) iBinder).getService(); // service is a private member of MainActivity
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.d(TAG, "Service disconnected !");
        }
    };
}

Running the application, Logcat shows :

mackosoft.almightymessage D/MainActivity﹕ Service binded ? false

And Service Connection is NEVER triggered !

I tried multiple solution :

  1. Moving the binding call to a Button.onClick() and using getApplicationContext()
  2. Using directly getApplicationContext()
  3. In the Manifest, changed -> android:name="mackosoft.almightymessage.MainService"
  4. Changed also the Intent-> final Intent intent = new Intent(this, MainService.MainServiceBinder.class);
  5. Also tried to add this.startService(intent) and with this.getApplicationContext()

All of the above fail and no error in the logs !

I have no luck at all.

Test device : Samsung Galaxy Note 3 running Cyanogen Mode 12.1 (Android 5.1)

Android Studio 1.2.1.1 (stable)

Thanks for the help !!


Solution

  • final Intent intent = new Intent(MainActivity.this, MainService.MainServiceBinder.class);
    

    Should be

    final Intent intent = new Intent(MainActivity.this, MainService.class);
    

    See the example provided in the docs for further info