Android: trouble with bindService() -> service is null - android

I'm having a problem with binding service to an activity. I get playing_service==null. I can't find what I'm doing wrong. Why is playing_service null??
MyActivity class:
private playService playing_service=null;
private ServiceConnection service_conn=new ServiceConnection(){
public void onServiceConnected(ComponentName className, IBinder service) {
LocalBinder binder=(LocalBinder)service;
playing_service=binder.getService();
}
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
}
};
public void playTrack(View view){
Intent i=new Intent(this,playService.class);
i.setAction("com.c0dehunter.soundrelaxer.PLAY");
bindService(i,service_conn,Context.BIND_AUTO_CREATE);
if(playing_service==null) //here I get true,
//if I try to access playing_service I get NullPointerException
}
}
playService class:
private final IBinder binder=new LocalBinder();
public int onStartCommand(Intent intent, int flags, int startId){
return 1; //dummy
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return binder;
}
public class LocalBinder extends Binder{
public playService getService(){
return playService.this;
}
}

Your service might not be null because binding a service is an asynchronous method, so instead of checking the availability of your service yet after calling the bind method, you should do it in your service connection implementation, for e.g.:
private ServiceConnection service_conn=new ServiceConnection(){
public void onServiceConnected(ComponentName className, IBinder service) {
LocalBinder binder=(LocalBinder)service;
playing_service=binder.getService();
if(playing_service != null){
Log.i("service-bind", "Service is bonded successfully!");
//do whatever you want to do after successful binding
}
}
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
}
};

Related

how can I get return value from android services

I got the problem that I don't know how can I fetch return value from services. Why I want return value from services is I want to show this return value in Activity page. Following is my services file and return value is retvalue.
public class SyncService extends Service{
private String forSync, lastrecordfromlocal, userdefined;
DatabaseUtil dbUtil = new DatabaseUtil(this);
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#SuppressWarnings("deprecation")
#Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
forSync = common.getInfoForSync(this);
String[] getlist = forSync.split(":");
lastrecordfromlocal = getlist[0].toString();
userdefined = getlist[1].toString();
if (common.isNetAvailable(this) == true) {
SyncServiceTask InstallData = new SyncServiceTask(this);
try {
String (((retvalue))) = InstallData.execute(lastrecordfromlocal, userdefined).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
this.stopSelf();
}
#Override
public void onDestroy() {
super.onDestroy();
}
}
As far as I know, the common way for service to communicate with activity is by BROADCAST.
You can send a broadcast at the position you want to "return retvalue" by something like:
Intent retIntent = new Intent(ServiceReturnIntent);
retIntent.putExtra("retvalue", retvalue);
sendBroadcast(retIntent);
which ServiceReturnIntent and "retvalue" are all self-defined strings to identify the broadcast and value name.
And in your activity which wishes to catch the return value, you should have a broadcast receiver, with something like:
public class myActivity extends Activity implements BroadcastReceiver {
TextView myTextView;
#Override
OnCreate(Bundle savedInstanceState) {
setContentView(R.layout.simple_layout);
myTextView = (TextView) findViewByID(R.id.simple_textview);
}
#Override
public void onReceive(Context context, Intent intent) {
int action = jumpTable.get(intent.getAction());
switch (action) {
case ServiceReturnIntent:
String valueFromService = intent.getStringExtra("retvalue");
myTextView.setText(valueFromService);
break;
// handle other action
......
}
}
You can read this tutorial for more about broadcast in android.
EDIT
modify sample code with setting it to a textview
You can bind service to the Activity:link
Your Service:
public class SyncService extends Service {
private final IBinder mBinder = new MyBinder();
private String;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
forSync = common.getInfoForSync(this);
String[] getlist = forSync.split(":");
lastrecordfromlocal = getlist[0].toString();
userdefined = getlist[1].toString();
if (common.isNetAvailable(this) == true) {
SyncServiceTask InstallData = new SyncServiceTask(this);
try {
String (((retvalue))) = InstallData.execute(lastrecordfromlocal, userdefined).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
return Service.START_NOT_STICKY;
}
#Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
public class MyBinder extends Binder {
SyncService getService() {
return SyncService .this;
}
}
public String getRetValue() {
return retvalue;
}
}
And in Activity check the value:
SyncService mService;
#Override
protected void onStart() {
super.onStart();
// Bind to LocalService
Intent intent = new Intent(this, SyncService .class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
#Override
protected void onStop() {
super.onStop();
// Unbind from the service
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
/** Called when a button is clicked (the button in the layout file attaches to
* this method with the android:onClick attribute) */
public void onButtonClick(View v) {
if (mBound) {
Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
}
}
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className,
IBinder service) {
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
if you want service to communicate with activity.we have two method.
1, you can use BROADCAST. you can send BROADCAST in service and use BroadcastReceiver in activity.
2, you also can use bindService to communicate activity.
public class LocalService extends Service {
// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
you can use iBinder to communicate with service

Android control a service by activity

For a chat application i need service which manages the communication between the user and the web service. I decided to use the usual Android service component.
I now how to to start the service, also i know how to send messages from the service to the activity, but how do i control the service from the activity?
E.g. the user sends a message, so i have to make the service to send e.g. http requests containing the message. Or the user wants to end a chat session, so the activity have to make the service to send a request containing a command to end the chat session.
You can use AIDL
http://developer.android.com/guide/components/aidl.html
In AIDL, you can implement methods in service, and call those methods from activity through binder.
So in your case,1) Implement sendMessage(String msg) in service
2)Declare this method in aidl file and call from service
mService.sendMessage(msg);
EDIT without using AIDL:
Service class
public class LocalService extends Service {
private final IBinder mBinder = new LocalBinder();
private final Random mGenerator = new Random();
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return mBinder;
}
public class LocalBinder extends Binder{
LocalService getService(){
return LocalService.this;
}
}
public int getRandomNumber()
{
return mGenerator.nextInt(100);
}
}
Activity class
public class BindingActivity extends Activity {
LocalService mService;
boolean mbound = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_binding);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_binding, menu);
return true;
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Intent intent = new Intent(this, LocalService.class);
bindService(intent,mConnection,Context.BIND_AUTO_CREATE);
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if(mbound)
{
unbindService(mConnection);
mbound = false;
}
}
private ServiceConnection mConnection = new ServiceConnection(){
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
LocalBinder binder = (LocalBinder)service;
mService = binder.getService();
mbound = true;
}
#Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
mbound = false;
}
};
public void onClick(View view)
{
if(mbound)
{
int num = mService.getRandomNumber();
Toast.makeText(this, "number: "+num, Toast.LENGTH_SHORT).show();
}
}
}

what happens when a service is started multiple times

I have created a simple service which will increment a counter and log it.
public class AudioService extends Service {
int i=0;
private final IAudioInterface.Stub mBinder = new IAudioInterface.Stub() {
public int getI()
{
return i;
}
};
#Override
public IBinder onBind(Intent intent) {
Log.d("onBind from service","****");
return mBinder;
}
#Override
public void onCreate() {
super.onCreate();
Log.d("onCreate from service","***");
Log.d("i=","="+i);
i=i+1;
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d("OnDestroy Called from service","***");
}
public void onStartCommand(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d("onStartCommand from service","***");
}
#Override
public boolean onUnbind(Intent intent) {
Log.d("onUnbind from service","***");
return super.onUnbind(intent);
}
i am incrementing the i value in the oncreate method of he service.
The Client Activity is as shown below
public class AidlInterfaceServiceActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private static final String TAG = "Audio:Service";
IAudioInterface mService = null;
boolean connected = false;
ServiceConnection mConnection = new ServiceConnection(){
public void onServiceConnected(ComponentName arg0, IBinder service) {
// TODO Auto-generated method stub
connected = true;
Log.d("Service connected","**");
mService = IAudioInterface.Stub.asInterface(service);
try {
Log.d("i from client","="+mService.getI());
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
Log.i(TAG, "Service disConnected");
connected = false;
mService= null;
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("on created ","**********");
Intent music = new Intent();
music.setClass(this,AudioService.class);
startService(music);
bindService(music, mConnection, Context.BIND_AUTO_CREATE);
}
The AIDL file is as below
interface IAudioInterface
{
int getI();
}
The problem is when i run the client activity every time the value of i is "0".
i value is not getting incremented.
I have these queries.
1.what happens when the service is started multiple time.
2.what happens when the service is bounded multiple times.
3.what happens to the data members inside the service class when it is started multiple times.Does those data members get reinitialized.
I am really confused ..Any one please help.
Does it help if you declare your counter variable as static?

Android Local Service Sample, bindservice(), and ServiceConnection()

I have a question which is related to this question that was asked by #mnish about a year ago.
Please have a look at his question and code. He implements a ServiceConnection() and passes it to bindService(). This follows the Local Service Sample in the Service documentation near the top.
I want to implement the Local Service Sample, so I am trying to add some details from #mnish question/answer. In ServiceConnection() #mnish has this line that confuses me:
mService = ILocService.Stub.asInterface(iservice);
I understand #mnish wrote this code, but does anybody have any idea what ILocService is and any idea how I might go about creating my own ILocService? Where is this construct documented and do I need it? Also where does the value for IBinder iservice come from?
He is probably using Android Interface Definition Language (AIDL)
http://developer.android.com/guide/components/aidl.html
Therefore he has to use a stub of the server side implementation like documented:
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. We are communicating with our
// service through an IDL interface, so get a client-side
// representation of that from the raw service object.
mService = IRemoteService.Stub.asInterface(service);
The iservice reference is coming from the onServiceConnected method which is called after binding the service to your activity. The call bindService gets passed the ServiceConnection which implements the onServiceConnected method.
You don't need the "IRemoteService.Stub.asInterface(service)" when your implementation of the service is local, then you can just cast the service to you local service.
The local service sample does this in the service:
public class LocalService extends Service {
private NotificationManager mNM;
// Unique Identification Number for the Notification.
// We use it on Notification start, and to cancel it.
private int NOTIFICATION = R.string.local_service_started;
/**
* 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 {
LocalService getService() {
return LocalService.this;
}
}
...
}
And this in the Activity in the ServiceConnection class:
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. Because we have bound to a explicit
// service that we know is running in our own process, we can
// cast its IBinder to a concrete class and directly access it.
mBoundService = ((LocalService.LocalBinder)service).getService();
// Tell the user about this for our demo.
Toast.makeText(Binding.this, R.string.local_service_connected,
Toast.LENGTH_SHORT).show();
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we should never
// see this happen.
mBoundService = null;
Toast.makeText(Binding.this, R.string.local_service_disconnected,
Toast.LENGTH_SHORT).show();
}
};
Here you are, my example .. will make you clear about that LOL
// My MyServiceInterface.aidl
package com.mad.exam;
interface MyServiceInterface {
int getNumber();
}
//MyService
public class MyService extends Service {
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "Service OnBind()", Toast.LENGTH_LONG).show();
return mBinder;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(this, "Service Created", Toast.LENGTH_SHORT).show();
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "Service Destroyed ", Toast.LENGTH_SHORT).show();
}
#Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(this, "Service Started ", Toast.LENGTH_SHORT).show();
}
#Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
}
private final MyServiceInterface.Stub mBinder = new MyServiceInterface.Stub() {
public int getNumber() {
return new Random().nextInt(100);
}
};
}
//My Activity
public class ServiceDemo extends Activity implements OnClickListener {
MyServiceInterface mService;
ServiceConnection mConnection;
Button retreive;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.service);
retreive = (Button) findViewById(R.id.retreive);
retreive.setOnClickListener(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);
try {
int i;
i = mService.getNumber();
Toast.makeText(ServiceDemo.this, "The service value is: " + String.valueOf(i), Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("My Tag", "Clicked");
Button btn = (Button) v;
Intent callService = new Intent(this, MyService.class);
bindService(callService, mConnection, Context.BIND_AUTO_CREATE);
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Intent callService = new Intent(this, MyService.class);
bindService(callService, mConnection, Context.BIND_AUTO_CREATE);
}
}

How to bind to running android service?

I'm hoping this is more of an issue with code than anything else and I'm hoping that someone out there can help track down the issue.
I have other code that starts the service with startService() and I can verify that the service is started as the debugger hits the onCreate() function of DecoderService.
However, the bindService never binds to the running service. Is this an asynchronous call and I have to wait for something to complete?
public class ResultsActivity extends Activity {
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Intent decoderIntent = new Intent(this,DecoderService.class);
_isBound = bindService(decoderIntent,mConnection,Context.BIND_AUTO_CREATE);
_textView.start(_mBoundService);
}
private boolean _isBound = false;
private DecoderService _mBoundService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
_mBoundService = ((DecoderService.LocalBinder)service).getService();
}
public void onServiceDisconnected(ComponentName className)
_mBoundService = null;
}
};
}
public class DecoderService extends Service {
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
_numCompleted = 5;
_numTotal = 100;
}
protected int _numCompleted = 0;
protected int _numTotal = 0;
public void onCreate() {
onHandleIntent(null);
}
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
DecoderService _ref = null;
public LocalBinder()
{
_ref = DecoderService.this;
}
DecoderService getService() {
return DecoderService.this;
}
}
}
Is this an asynchronous call and I
have to wait for something to
complete?
The binding is not ready until onServiceConnected() is called on your ServiceConnection object.

Categories

Resources