Dear all,
I have an app that contains Activity A and MyService which this
service runs in background indefinitely. Every time Activity A call
onStart i bind this activity to myservice in order to pass data from
Activity A to myservice and Activity A unbind to myservice when it
calls onStop. Everything works ok for 3 days , but now activity A
cannot bind to myservice every time it call onStart anymore. myservice
is still running in background, i know this because i see myservice in
list of service return from
ActivityManager.getRunningServices(Integer.MAX_VALUE).
So what is happening to myservice? it has worked well but now not
correct anymore. this activity A can only bind to myservice again by
calling StartService() again, this will call onCreate on myservice.I
don't want to call startService() again because this is an indefinite
service. Assuming that i have to call startService() again, i dont
even know that when and where i will call startService() again.
/
Here is my code of Activity A:
class ActivityA extends Activity {
Messenger mService;
...
@Override
protected void onStart() {
// TODO Auto-generated method stub
Log.i(TAG, onStart);
super.onStart();
startServiceInBackground();
}
private void startServiceInBackground(){
Intent intent = new Intent(getBaseContext(), MyService.class);
if(isMyServiceRunning()){
Log.i(TAG, startServiceInBackground already);
if(!mIsBound){
doBindService();
}
}
else {
// intent.setAction(com.android.test.MY_SERVICE);
Log.i(TAG, startServiceInBackground first time);
startService(intent);
doBindService();
}
}
private boolean isMyServiceRunning() {
ActivityManager manager =
(ActivityManager)getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service :
manager.getRunningServices(Integer.MAX_VALUE)) {
if(Constant.MYSERVICE_CLASS_NAME.equals(service.service.getClassName()))
{
return true;//find myservice in list of running
services
}
}
return false;
}
void doBindService() {
Log.i(TAG, doBindService);
bindService(new Intent(ActivityA.this, MyService.class),
mConnection,0);
//0:mean that i don't need to call onCreate() of service once
again because it has already existed after calling //
startService()
mIsBound = true;
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
Log.i(TAG, onServiceConnected);
mService = new Messenger(service);
try {
/send data/
mService.send(somedata);
} catch (RemoteException e) {
}
}
public void onServiceDisconnected(ComponentName className)
{
Log.i(TAG, onServiceDisconnected);
}
};
@Override
protected void onStop() {
// TODO Auto-generated method stub
Log.i(TAG, onStop);
super.onStop();
try {
doUnbindService();
} catch (Throwable t) {
Log.w(TAG, Failed to unbind from the service, t);
}
}
void doUnbindService() {
Log.i(TAG, doUnbindService);
if (mIsBound) {
// Detach our existing connection.
unbindService(mConnection);
mIsBound = false;
}
}
}
and here is code of myservice:
//
public class MyService extends Service{
final Messenger mMessenger = new Messenger(new
IncomingHandler());
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.i(TAG,function onCreate() called );
super.onCreate();
//do something
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Log.i(TAG, onStart start id + startId + , intent
= + intent);
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int
startId) {
// TODO Auto-generated method stub
Log.i(TAG, onStartCommand start id + startId + ,
intent = + intent);
return START_STICKY;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i(TAG, onDestroy);
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, onBind intent = + intent);