I am reading upon Android Bound service, http://developer.android.com/guide/components/bound-services.html
public class LocalService extends Service {
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
// Random number generator
private final Random mGenerator = new Random();
/**
* Class used for the client Binder. 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 {
LocalService getService() {
// Return this instance of LocalService so clients can call public methods
return LocalService.this;
}
}
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
/** method for clients */
public int getRandomNumber() {
return mGenerator.nextInt(100);
}
}
And all the tutorial, android developer guide and books suggest to have Binder as inner class of service. Is it really have to be only inner class ?
It is an inner class so you can return the outer Service instance easily. You could als make it an external class:
public class LocalBinder extends Binder {
private final LocalService mLocalService;
public LocalBinder(final LocalService service) {
mLocalService = service;
}
LocalService getService() {
return mLocalService;
}
}
Using an inner class saves you from the trouble of creating a field and a constructor.
Related
I am trying to write Unit Test for Android Service using JUNIT4.
Following my service code:
class myService extends Service {
// Binder given to clients
private IBinder mBinder = null;
#Override
public void onCreate() {
super.onCreate();
mBinder = new LocalBinder();
}
public int multiply(int x, int y){
return (x*y);
}
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public class LocalBinder extends Binder {
public myService getService() {
// Return this instance of LocalService so clients can call public methods.
return myService.this;
}
}
}
And My Unit test Code:
public class testServiceUlt {
#Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
#Test
public void testWithBoundService() throws TimeoutException, InterruptedException {
// Create the service Intent.
Intent serviceIntent =
new Intent(InstrumentationRegistry.getTargetContext(), myService.class);
// Bind the service and grab a reference to the binder.
IBinder binder = mServiceRule.bindService(serviceIntent);
// Get the reference to the service, or you can call public methods on the binder directly.
myService service = ((myService.LocalBinder) binder).getService();
int val = service.multiply(80, 0);
assertEquals("should be zero", 0, val);
}
}
The problem is that the binder is Null and I got the following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'com.example.xxx.xxx.xxx com.example.xxx.xxx.xxx$LocalBinder.getService()' on a null object reference
Can you please advice what is my problem?
P.S - I know how to do it using Junit3 with ServiceTestCase, but I would like to do with Junit4
Thanks,
Zachi
I managed to solve the problem.
By mistake my AndroidManifest.xml file did not include the Service.
In my case the problem was that I declared the Service in the androidTest Manifest file, which isn't working for any reason.
In the developer docs for Bound Services, the following code example is given for "Extending the Binder Class" in "Creating a Bound Service". The following code snippet (I have removed irrelevant bits) is given in which the Service returns an IBinder from its onBind()method:
public class LocalService extends Service {
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
...
/**
* Class used for the client Binder. 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 {
LocalService getService() {
// Return this instance of LocalService so clients can call public methods
return LocalService.this;
}
}
#Override
public IBinder onBind(Intent intent) {
return mBinder; //**********************************************************
}
...
}
Then in our client, we receive the mBinder object (which is an instance of LocalBinder) in the onServiceConnected() method of ServiceConnection. My question is that why are we trying to cast an instance of LocalBinder passed in as an argument to onServiceConnected() into a LocalBinder instance in the statement LocalBinder binder = (LocalBinder) service; ?
public class BindingActivity extends Activity {
LocalService mService;
boolean mBound = false;
...
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
...
}
The definition of ServiceConnection.onServiceConnected() is
public void onServiceConnected(ComponentName className, IBinder service)
Note that the parameter is an IBinder - ServiceConnection does not know what kind of service or what kind of IBinder implementation the service is returning - only you know that, hence why you need to cast it to the correct type.
Because the only type information you have in onServiceConnected is that you get an object of type IBinder. IBinders don't have a getService method so you must perform a cast of the IBinder object to an object of type LocalBinder. Then you can call the getService method. This is how static typing works.
Background: I'm running a background service (independent of the app opened or not) to maintain connection with Tizen-based app on Gear2 (not Android, hence the manual maintenance).
Whenever my phone apps (multiple apps) have data to send to send to the service, I need to get the 'connection' object inside the service and call 'send'.
So my question is: how can I get running service object?
If I can get that service, my code will be like this:
MyConnection connection = runningService.getConnection()
connect.send(message);
Thanks.
If it's only a single object (say connection) you need to periodically access, I would probably make it to be a singleton, which is created by the services and available to the other components of your app:
class MyConnection {
private static MyConnection inst;
public static void set(........) { <-------- set by service
}
public static getInstance() { return inst; } <------- and accessible to other components
}
But, if you need a more elaborate and continuous interaction with your service, you should probably set it to
be a bound service, and hand craft the interface you would like it to implement:
Create a Bound Service:
class MyConnectionService extends Service {
private final IBinder myBinder = new MyLocalBinder();
#Override
public IBinder onBind(Intent arg0) {
return myBinder;
}
public ConnectionRecord getConnection() {
return myConnection;
}
public class MyLocalBinder extends Binder {
MyConnectionService getService() {
return MyConnectionService.this;
}
}
}
And bind to it from another component, e.g. an Activity:
public class MyActivity extends Activity {
MyConnectionService serviceConnector;
boolean isBound = false;
private ServiceConnection serviceConnector = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
MyLocalBinder binder = (MyLocalBinder) service;
serviceConnector = binder.getService(); //<--------- from here on can access service!
isBound = true;
}
public void onServiceDisconnected(ComponentName arg0) {
serviceConnector = null;
isBound = false;
}
};
.
.
.
}
Note that after onServiceConnected() is completed you will have a serviceConnector object you can use to communicate
with the service, which is what we aimed for.
you cannot have multiple instance of a service. so you just need to send commands to it, via startService().
Ok, I'm new to android development and am trying to bind to a service so that I can call methods on the service once it's been started. The Activity and Service described below are both part of the same application so there shouldn't be any problems there, but everytime I run my app I get the following error:
java.lang.ClassCastException: android.os.BinderProxy
The line this happens on is:
LocalBinder binder = (LocalBinder) service;
My Activity code (simplified is):
public class Main extends Activity {
boolean gpsBound = false;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/** Called whenever the activity is started. */
#Override
protected void onStart() {
super.onStart();
// Bind to GPSService
Intent i = new Intent(this, GPSService.class);
startService(i);
bindService(i, connection, Context.BIND_AUTO_CREATE);
}
/** service binding */
private ServiceConnection connection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// After binding to GPSService get the instance of it returned by IBinder
LocalBinder binder = (LocalBinder) service;
gpsBound = true;
}
public void onServiceDisconnected(ComponentName className) {
gpsBound = false;
}
};
}
Service:
public class GPSService extends Service {
#Override
public void onCreate() {
super.onCreate();
}
#Override
public IBinder onBind(Intent i) {
// TODO Auto-generated method stub
return new LocalBinder<GPSService>(this);
}
/**
* Our implementation of LocationListener that handles updates given to us
* by the LocationManager.
*/
public class CustomLocationListener implements LocationListener {
DBHelper db;
CustomLocationListener() {
super();
}
// Overridden methods here...
}
}
And finally my LocalBinder:
/**
* A generic implementation of Binder to be used for local services
* #author Geoff Bruckner 12th December 2009
*
* #param <S> The type of the service being bound
*/
public class LocalBinder<S> extends Binder {
private String TAG = "LocalGPSBinder";
private WeakReference<S> mService;
public LocalBinder(S service){
mService = new WeakReference<S>(service);
}
public S getService() {
return mService.get();
}
}
I understand the meaning of the ClassCast Exception but cannot understand what to do! I've followed the example in the google documentation but it's still not working. Can anyone shed any light on what might be causing this?
Thanks in advance!
Delete attribute process in your AndroidManifest.xml of your service.
Had same error. I had added the android:process=":process_description" attribute in the manifest. When you add it, your service is created as separate process and hence you get instance of binderProxy (Hence the class cast exception)
If you are trying to bind to a local service than yes, you can just cast it. However if you are trying to bind to a remote (separate process) service you must use the AIDL method as prescribed in this article.
http://developer.android.com/guide/components/aidl.html
the LocalBinder passed in onServiceConnected has a generic type argument, while your local variable LocalBinder binder does not have one.
Resolve this one way or another, either by removing the generic type from the definition of LocalBinder, or by adding one to your declaration of your local variable binder in onServiceConnected
class MyBoundService extends Service{
private final IBinder mBinder = new MyBinder();
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public class MyBinder extends Binder{
public void doStuff(){
//Stuff
}
//More Binder Methods
}
}
class MyActivity extends Activity{
private MyBinder mBinder;
#Override
protected void onStart(){
Intent intent = new Intent(this, MyBoundService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
#Override
protected void onStop(){
unbindService(mConnection);
}
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className, IBinder service) {
mBinder = (TaskBinder) service;
mBound = true;
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
private void doStuff(){
if (mBound)
mBinder.doStuff();
}
}
No real need to fiddle around with weak references and whatnot. just be sure to unbind (I didn't in the sample)
If you want to invoke service methods ASAP, just put calls in onServiceConnected, after you set mBinder. otherwise, just invoke from other callbacks (onClick events and whatnot).
EDIT: The code below has been edited to show the correct solution to the problem.
I have an app which uses a foreground service to perform network operations.
Currently, the foreground service uses a bluetooth connection to perform the operations. I'm trying to implement a new version of the service which uses wifi instead, and allow the user to decide whether to use bluetooth or wifi through shared preferences.
I've implemented the wifi service, and now I need to bind to it. I created an interface, MyService, which defines all of the methods that both versions of the service require. However, when I try to bind to the service in my activity, I get a ClassCastException error.
Here are the relevant parts of my service interface:
MyService.java:
public interface MyService {
// constants
...
// method declarations
...
public interface LocalBinder {
MyService getService(Handler handler);
}
}
And here are the relevant methods which are present in both versions of the service:
MyBluetoothService.java:
public class MyBluetoothService extends Service implements MyService {
private final IBinder mBinder = new LocalBinder();
...
public class LocalBinder extends Binder implements MyService.LocalBinder {
MyService getService(Handler handler) {
mHandler = handler;
// Return this instance of MyService so clients can call public methods
return MyBluetoothService.this;
}
}
#Override
public IBinder onBind(Intent intent) {
Log.w(TAG, "MyBluetoothService bound");
return mBinder;
}
}
MyWifiService.java: Exactly the same as MyBluetoothService.java except with class names changed as necessary.
And here is where I bind to the service in my activity:
MyService mChatService = null;
...
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to MyService, cast the IBinder and get MyService instance
LocalBinder binder = (LocalBinder)service; <------- ClassCastException
mChatService = binder.getService(mHandler);
mBound = true;
}
#Override
public void onServiceDisconnected(ComponentName argo) {
mBound = false;
}
};
The ClassCastException occurs on the line indicated above.
Now that all of that is out of the way... is it possible to bind to a service in this way? Alernatively, I could always check shared preferences every time I call a method from the service but I'd rather not.
I'm assuming that the code where it is throwing is a MyService.LocalBinder class and not a MyBluetoothService.LocalBinder class?
What I think you meant to do is define the MyBluetoothService.LocalBinder class to extend from the MyService.LocalBinder class?
e.g.
public class MyBluetoothService extends Service implements MyService {
private final IBinder mBinder = new LocalBinder();
...
public class LocalBinder extends MyService.LocalBinder {
MyService getService(Handler handler) {
mHandler = handler;
// Return this instance of MyService so clients can call public methods
return MyBluetoothService.this;
}
}
#Override
public IBinder onBind(Intent intent) {
Log.w(TAG, "MyBluetoothService bound");
return mBinder;
}
}