Gerardo
Gerardo

Reputation: 5830

Android bindService problem

i have a problem with bindService. In my Activity i have the following code:

private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className,
            IBinder service) {
        mService = IPrimary.Stub.asInterface(service);
    }

    public void onServiceDisconnected(ComponentName className) {
        mService = null;
    }
};
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.login);
    mApi = new ApiRequest(SIGNIN_METHOD);
    boolean isConnected = bindService(new Intent(IPrimary.class.getName()),
            mConnection, Context.BIND_AUTO_CREATE);

But isConnected is equals to false every time.

In my manifest file i have:

        <service android:name=".DownloaderService">
  <intent-filter>
<action android:name=".IPrimary" />

si i don't understand the problem. In logcat appears:

I/ActivityManager( 52): Displayed activity com.touristeye.code/.LogIn: 485918 ms (total 913151 ms)

Thank you

Upvotes: 2

Views: 5705

Answers (2)

George Nguyen
George Nguyen

Reputation: 2169

You shouldn't do that:

 boolean isConnected = bindService(new Intent(IPrimary.class.getName()), mConnection, Context.BIND_AUTO_CREATE);

Please put the code when you handle service in private ServiceConnection mConnection = new ServiceConnection() {} ... I calls back and you have the service to handle there We're not sure when the service is actually bond until we got callback from ServiceConnection

Here is the flow

Create your intent to call a service. You can either startService() or BindService() with BIND_AUTO_CREATE

Once the service is bond, it will create a tunnel to talk with it clients which is the IBinder Interface. This is used by your AIDL Interface implementation and return the IBinder in

private final MyServiceInterface.Stub mBinder = new MyServiceInterface.Stub() {
    public int getNumber() {
        return new Random().nextInt(100);
    }
};

public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "Service OnBind()", Toast.LENGTH_LONG).show();
    return mBinder;
}

Once it returns the mBinder, ServiceConnection that you created in the client will be called back and you will get the service interface by using this

           mConnection = new ServiceConnection() {

        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub

        }

        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub

            mService = MyServiceInterface.Stub.asInterface(service);


    };

Now you got the mService interface to call and retreive any service from that

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007659

Expand that action:name to be the full value in the <action> element. It may be that the dot-prefix shorthand only works for the component element (e.g., <service>).

Upvotes: 3

Related Questions