I am trying to associate an Activity with a LocalService in order to interact with it. But in my activity I can only call methods defined in my LocalBinder, and not in my LocalService. What am I doing wrong?
Don't start from scratch I read another question , and I read a little how to enter the sample code , and my code is similar to this sample code. In addition, I read some of the Service Documentation for convenience, here is a small quote from this section of the documentation:
βA service is bound when an application component communicates with it, calling bindService (). The linked service offers a client-server interface that allows components to interact with the service, send requests, receive results, and even do this between processes with interprocess communication (IPC). A linked service is started only when another component of the application is bound to it. Several components can immediately communicate with the service, but when they all turn off, the service is destroyed. "
But I canβt do it. As mentioned above, the best thing I can do is to have my Activity call methods defined in my LocalBinder. I did not achieve anything, as the part highlighted in black over.
If this helps, these are the relevant parts of my code.
The local service should be tied to:
package com.marie.localservicesample; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.UUID; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.Intent; import android.os.Bundle; import android.os.IBinder; import android.os.Looper; import android.os.Message; import android.os.Messenger; import android.os.RemoteException; import android.util.Log; import android.widget.Toast; public class LocalService extends Service { private NotificationManager mNM;
Activity that binds to LocalService:
package com.marie.localservicesample; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class Binding extends Activity { private ILocalBinder mBoundService; private boolean mIsBound; private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) {
My ILocalBinder and LocalBinder:
/************************************************************************************************** * Filename: ILocalBinder.java * Project name: Local Service Sample * Application name: Local Service * Description: This file contains an example interface for my LocalBinder **************************************************************************************************/ package com.marie.localservicesample; public interface ILocalBinder { public int getStatusCode(); } /************************************************************************************************** * Filename: LocalBinder.java * Project name: Local Service Sample * Application name: Local Service * Description: This file contains the LocalBinder class for our Local Service application **************************************************************************************************/ package com.marie.localservicesample; import android.os.Binder; import com.marie.localservicesample.LocalService; /** * Class for clients to access. Because we know this service always * runs in the same process as its clients, we don't need to deal with * IPC. */ public class LocalBinder extends Binder implements ILocalBinder { @Override public int getStatusCode() { return LocalService.statusCode; } }
Thanks!