In the Service class:
class AeroBluetoothService extends Service { ...
private final IBinder asBleBinder = new LocalBinder();
public class LocalBinder extends Binder {
AeroBluetoothService getService() {
return AeroBluetoothService.this;
}
}
#Override
public IBinder onBind(Intent intent) {
return asBleBinder;
}
In the client Activity:
Intent bindAsBleIntent;
#Override
public void onServiceConnected( ComponentName className, IBinder service ) {
AeroBluetoothService.LocalBinder asBleBinder = (AeroBluetoothService.LocalBinder) service;
asBleServiceRef = asBleBinder.getService();
}
In Client's onCreate():
bindAsBleIntent = new Intent( getApplicationContext(), AeroBluetoothService.class );
bindService( bindAsBleIntent, /*ServiceConnection*/ this, Context.BIND_AUTO_CREATE );
The problem is that when I try to call a Service method from the Client:
asBleServiceRef.scanForAero();
the reference to the Service instance, asBleServiceRef, is null. It is as though the onServiceConnected() callback is not being called (or is passing a null argument).
I copied this code quite carefully from an Android example. I just noticed that the example calls bindService() from its onStart() method, whereas I'm calling from onCreate(). Could that make any difference? What's the problem?
As #avinash correctly saw, the problem was simply neglecting to declare the Service in the Manifest.
Related
I have a Service which is already bound by an external App via AIDL.
However, there are some service requests which require to start an Activity.
Since I cannot call startActivityForResult from within a Service I decided to Bind my local Activities to the service as well.
(PseudoCode) looks like this:
class MyService extends Service{
public IBinder onBind(Intent intent){
if (intent.hasExtra("LocalBindingRequest")){
return getLocalBinder();
else {
return getAidlBinder();
}
}
}
class ExternalApp extends Activity{
void someFunc(){
Intent i = new Intent(new ComponentName("com.my.pkg", "com.my.pkg.MyService");
bindService(i, myServiceConnection, Context.BIND_AUTO_CREATE);
}
}
class InternalApp extends Activity{
MyService mService;
void someFunc(){
Intent i = new Intent(new ComponentName("com.my.pkg", "com.my.pkg.MyService")
.putExtra("LocalBindingRequest", true);
bindService(i, myServiceConnection, Context.BIND_AUTO_CREATE);
}
public void onServiceConnected(ComponentName cn, IBinder service){
InternalBinder ib = (LocalBinder)service;
mService = ib.getService();
}
}
Flow is like this:
ExternalApp binds to AidlBinder
ExternalApp calls Function which requires Service to start an Activity
Service starts Activity
Internal Activity tries to Bind
I get an Exception (appearantly without hitting a breakpoint in onBind or onServiceConnected)
java.lan.ClassCastException: AidlService cannot be cast to InternalBinder
Isn't it possible for a Service to return a different Binder?
If not, what can I do, to propagate a Result back to MyService which is already bound?
Ok I should have read the docs stating in onBind(Intent)
Intent: The Intent that was used to bind to this service, as given to
Context.bindService. Note that any extras that were included with the
Intent at that point will not be seen here.
Thats why I was given the Aidl Service. The fix would be:
class InternalApp extends Activity{
MyService mService;
void someFunc(){
Intent i = new Intent(new ComponentName("com.my.pkg", "com.my.pkg.MyService");
i.setAction("LocalBindingRequest");
bindService(i, myServiceConnection, Context.BIND_AUTO_CREATE);
}
public void onServiceConnected(ComponentName cn, IBinder service){
InternalBinder ib = (LocalBinder)service;
mService = ib.getService();
}
}
class MyService extends Service{
public IBinder onBind(Intent intent){
if ("LocalBindingRequest".equals(intent.getAction()){
return getLocalBinder();
else {
return getAidlBinder();
}
}
}
And we can have separate Binders for each binding request
I would like to have a service (doing occasional monitoring) be active continuously. I plan to start it by listening to a BOOT_COMPLETE, which I believe makes it a "Started Service". I want a UI application to bound to it, which is working and documented. However, after the binding activity is destroyed, the Service dies because it's "unbound from all clients".
Is there a way to have a started service allow binding and still continue after the last bound services un-binds?
Returning true from onUnbind() wouldn't help, as the service should continue to be active even if no additional binder exist.
In Android, services are started in one of two ways - through the startService(Intent i) method, or the bindService(Intent i). The method used to start the service determines whether it is started or bound. A service can be started, then bound to a client - or bound and then have calls to start sent to it (it doesn't restart if already started).
As you mention listening for BOOT_COMPLETE, I assume this is an action for an Intent, which is sent via a Broadcast object. This means that you can create an IntentFilter object with the BOOT_COMPLETE action added to it via the addAction(String action) method. Then a BroadcastReceiver object can be created, which upon receiving an intent with an action of BOOT_COMPLETE can then call the startService(Intent i) method (this is done by overriding the onReceive(Context context, Intent intent) method).
If you call startService(Intent i) when the Intent is received, then the service will be a started service. This means that it will only stop when a call to stopService(Intent i) is made by the app, or if the service calls the stopSelf() method. It can be bound and unbound from by multiple activities during the time it is running, and it will not stop (as it is started, not bound).
Here is an example of this, using two Activity objects and a Service:
Activity 1 (first activity of your app):
public class ServiceActivity extends Activity {
private IntentFilter filter = new IntentFilter();
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if(action.equals(BOOT_COMPLETE) {
startService(new Intent(ServiceActivity.this, MyService.class));
}
}
};
#Override
protected void onStart() {
super.onStart();
filter.addAction(BOOT_COMPLETE);
registerReceiver(receiver, filter);
}
#Override
protected void onStop() {
super.onStop();
unregisterReceiver(receiver);
}
//Some other code
}
Activity 2 (used at some point after activity 1):
public class AnotherActivity extends Activity {
private MyService service;
private ServiceConnection connection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
service = ((MyService.MyBinder)service).getService();
}
#Override
public void onServiceDisconnected(ComponentName name) {
service = null;
}
};
#Override
protected void onStart() {
super.onStart();
bindService(new Intent(this, MyService.class), connection, Context.BIND_AUTO_CREATE);
}
#Override
protected void onStop() {
super.onStop();
unbindService(connection);
}
//Some other code
}
Service:
public class MyService extends Service {
private MyBinder binder = new MyBinder();
#Override
public IBinder onBind(Intent intent) {
return binder;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
//Some other code
final class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
Final notes
To be able to use the service as bound, you need to override the onBind(Intent intent) method, and return an instance of binder MyBinder. Not doing so will result in not being able to bind (the binding sets the service variable by using the getService() method defined in MyBinder).
The BroadcastReceiver must alwasy be unregistered when the Activity it's in closes, as it would be leaked otherwise. That is why in the example, I have registered and unregistered in the onStart() and onStop() methods respectively. Using onDestroy() to unregister is not recommended as it is not always called.
The MyService object that is used when binding must also be unbound when it's Activity closes, as it too can be leaked. It is set to null when onServiceDisconnected(ComponentName name) is called for garbage collecting.
Sources for further reading
https://developer.android.com/reference/android/content/BroadcastReceiver.html
https://developer.android.com/reference/android/app/Activity.html
https://developer.android.com/reference/android/app/Service.html
https://developer.android.com/reference/android/content/ServiceConnection.html
Let me start by saying I've been searching for a long time, found a lot of similar questions (on SO) but I can't find anything to solve this yet:
I have a Service (jobcrawler) that is started by calling startservice().
Within this service, I am starting a long-running thread, which at some point is calling a class (webservice) whose init looks like this:
public webservice(Context context) {
this.context = context;
this.db = new DatabaseHandler(this.context);
this.access_token = db.getAuthKey();
}
After some network calls, the class(webservice) receives data in a method called recieveData().
Inside recieveData I am attempting to bind to the service as follows:
if (!isBound) {
// not bound yet, then bind to the service.
Intent intent = new Intent(this, jobcrawler.class);
bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
}
Now, I'm getting nullpointerexemption on the line where I call bindservice. Note, that I'm not actually attempting to do anything with the service yet. I'm just trying to bind to it.
any help would be appreciated... if I had hair I'd be pulling it out! lol
Here's some additional code that I think is relevant.
myConnection:
private ServiceConnection myConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,IBinder service) {
Log.e("webservice", "service is connected");
MyLocalBinder binder = (MyLocalBinder) service;
myService = binder.getService();
isBound = true;
}
public void onServiceDisconnected(ComponentName arg0) {
Log.e("webservice", "service is disconnected");
isBound = false;
}
};
binder from service called MyLocalBinder:
public class MyLocalBinder extends Binder {
public jobcrawler getService() {
Log.e("Job Crawler", "returning self");
return jobcrawler.this;
}
}
service's onbind method:
private final IBinder myBinder = new MyLocalBinder();
#Override
public IBinder onBind(Intent arg0) {
Log.d("JobCrawler Service", "Service is bound");
return myBinder;
}
oh and this is where I load the class from the thread inside the service, just in case I should be using a different context or something:
private webservice ws= new webservice(getBaseContext());
I know it's a bit late, but I ran upon the same problem and maybe some googlers will be happy :)
So for me the following worked:
Call the bindService method in reference to your context:
context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
I'm trying to bind a service, but onBind() always returns false.
This is the code for the ServiceConnection-
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with our service has been established,
// giving us the service object we can use to interact with our service.
mBoundService = ((ScheduleService.ServiceBinder) service).getService();
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
This is call to bindService() -
boolean test = getApplicationContext().bindService(new Intent(this, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);
This is the declaration of the service in the Manifest -
<service android:name=".Notifications.ScheduleService" android:enabled="true"/>
I've read previous questions regarding the subject and couldn't find an answer(tried switching the Activity context with the Application context, but it didn't help).
I'm using using Frgaments and ActionBarSherlock, and my Activity extends SlidingFragmentActivity (That's why i'm using the application context, which doesn't help).
Edit - This is the code of the service i'm trying to start -
public class ScheduleService extends Service {
/**
* Class for clients to access
*/
public class ServiceBinder extends Binder {
public ScheduleService getService() {
return ScheduleService.this;
}
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("ScheduleService", "Received start id " + startId + ": " + intent);
// We want this service to continue running until it is explicitly stopped, so return sticky.
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
// This is the object that receives interactions from clients. See
private final IBinder mBinder = new ServiceBinder();
/**
* Show an alarm for a certain date when the alarm is called it will pop up a notification
*/
public void setAlarm(Calendar c) {
// This starts a new thread to set the alarm
// You want to push off your tasks onto a new thread to free up the UI to carry on responding
new AlarmTask(this, c).run();
}
}
Any help will be appreciated . Thanks.
What is the fully qualified class-name of ScheduleService (i.e. including the full package-name)?
I'm asking this, because in your AndroidManifest.xml file, your service's name is .Notifications.ScheduleService, which seems a bit odd:
This tells me that either
The (last part of the) package-name contains a upper-case
character... not so good.
I would expect .notifications.ScheduleService instead, if this is the case.
The ScheduleService is defined within a file called Notifications.java.
I would expect .Notifications$ScheduleService instead, if this is the case (dollar sign instead of period).
Do you mean bindService() returns false? onBind() returns IBinder type.
Keep in mind that service binding takes some time. If you want to perform some action after binding is done you can perform it in the onServiceConnected() method.
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((ScheduleService.ServiceBinder) service).getService();
Calendar c = new Calendar();
mBoundService.setAlarm(c);
}
If you need more guidance on this you need to show us your Activity code.
Why do you use the application context to bind the service?
The method bindService is called through the ContextWrapper. It might not be the issue but I'd share contexts across the place where you bind the service and where you have the connection.
In your case instead of
boolean test = getApplicationContext().bindService(new Intent(this, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);
I'd do the following
boolean test = bindService(new Intent(this, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);
Or if you want to keep a global context within the application, move everything to your Application file and call it similarly the same way suggested above.
The issue can also be on the package name of your app and the declaration of your service in your manifest. If you are unsure make sure to give the global route to your service in the manifest.
I have an Android service, created in OnCreate of first Activity of the application using StartService(). I need this service to be running throughout the life span of the application ie, all the activities in the application. But the service should not consume the resources after user pressed Home key or Back button. Is there any elegant way to do that other than stopping the service in onPause() method of all the activities?
Instead of using StartService, you can call bindService in onResume and unbindService in onPause. Your service will stop when there are no open bindings.
You'll need to create a ServiceConnection to get access to the service. For instance, here's a class nested inside MyService:
class MyService {
public static class MyServiceConnection implements ServiceConnection {
private MyService mMyService = null;
public MyService getMyService() {
return mMyService;
}
public void onServiceConnected(ComponentName className, IBinder binder) {
mMyService = ((MyServiceBinder)binder).getMyService();
}
public void onServiceDisconnected(ComponentName className) {
mMyService = null;
}
}
// Helper class to bridge the Service and the ServiceConnection.
private class MyServiceBinder extends Binder {
MyService getMyService() {
return MyService.this;
}
}
#Override
public IBinder onBind(Intent intent) {
return new MyServiceBinder();
}
#Override
public boolean onUnbind(Intent intent) {
return false; // do full binding to reconnect, not Rebind
}
// Normal MyService code goes here.
}
One can use this helper class to get access to the service via:
MyServiceConnection mMSC = new MyService.MyServiceConnection();
// Inside onResume:
bindService(new Intent(this, MyService.class), mMSC, Context.BIND_AUTO_CREATE);
// Inside onPause:
unbindService(mMSC);
// To get access to the service:
MyService myService = mMSC.getMyService();
You could do what Darrell suggests but put that code in a new class that extends Activity and then extend that on all your normal Activities.
I don't know any other more elegant way of achieving your goals.