0

My problem is in the question title, i'm trying to start/bind a simple service with just log in it and it's not working at all. I want to do it with Service Connexion method :

public class testServiceBound extends Service {
    /**
     * PRIVATE ATTRIBUTES
     */
    // log
    private static final String TAG     = testServiceBound.class.getSimpleName();
    private final Binder        _binder = new testBinder();

    @Override
    public void onCreate() {

        Log.i(TAG, "onCreate--");
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.i(TAG, "onStartCommand--");
        return START_STICKY;
    }

    @Override
    public void onDestroy() {

        Log.i(TAG, "onDestroy--");
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {

        Log.i(TAG, "onBind--");
        return _binder;
    }

    public class testBinder extends Binder {
        public testServiceBound getServiceInstance() {

            Log.i(TAG, "PlayerBinder--getServiceInstance--");
            return testServiceBound.this;
            }        
    }
}

In the activity :

private testServiceBound        _testService;

public final ServiceConnection  _connectionStreamingtest    = new ServiceConnection() {
                                                                @Override
                                                                public void onServiceConnected(ComponentName className, IBinder testBinder) {

                                                                    Log.i(TAG, "onServiceConnected--");
                                                                    // service
                                                                    testBinder binder = (testBinder) testBinder;
                                                                    _testService = binder.getServiceInstance();
                                                                }

                                                                @Override
                                                                public void onServiceDisconnected(ComponentName arg0) {

                                                                    Log.i(TAG, "onServiceDisconnected--");
                                                                    _bound = false;
                                                                }
                                                            };
public void bindToService() {

    Log.i(TAG, "bindToService--");
    Intent intenttest = new Intent(MainActivity.this, testServiceBound.class);
    startService(intenttest);
}

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.i(TAG, "onCreate--");
    //
    // service
    bindToService();
}

In the manifest :

<service
    android:name="com.egdigital.testing.service.testServiceBound"
    android:enabled="true"
    android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
</service>

and i tried with this too

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

No error, no log, nothing happens..

An-droid
  • 6,433
  • 9
  • 48
  • 93

2 Answers2

1

You are starting the service explicitly and not binding. So your service connection code won't be firing.

Try this code instead:

Intent intenttest = new Intent(MainActivity.this, testServiceBound.class);
bindService(intenttest , _connectionStreamingtest   , Context.BIND_AUTO_CREATE);
  • This is one thing i don't understand about services, if i want to start it (and onBind return null) i just do StartService OK but if i want to bind a "sticky" service i just need to call bindService and it ll start ? – An-droid Jul 16 '13 at 08:08
  • I used Context.BIND_AUTO_CREATE in the bindservice call so that when you bind to the service it will be created automatically if it needs to be. If there are no more binders to the service it will end unless explicitly started. – metalmonkeysoftware Jul 16 '13 at 14:32
  • i see, thanks for the information. I finally found what was wrong (see my response) – An-droid Jul 16 '13 at 14:35
0

OK at least i found ! Thanks for all the comments and answers =)

<service
    android:name="com.egdigital.testing.service.testServiceBound"
    android:enabled="true"
    android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
</service>

this was not in the application tag !! And of course it needs to be there (1 day to find this..)

An-droid
  • 6,433
  • 9
  • 48
  • 93