[android-developers] problem with the indefinite service

2011-11-11 Thread phannguyen
Dear all,
My app has an activity A and i want to create MyService which can run
in background indefinitely. So from activity A, i call start myservice
as following:
Intent intent = new Intent(getBaseContext(), MyService.class);
startService(intent);

I also want to pass data object from Activity A to MyService during
the running time of myservice, so i bind Activity A to MyService as
below :
bindService(new Intent(ActivityA.this, MyService.class), mConnection,
0);
with mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
 //get binder and send data to myservice
}
In onStop event of Activity A i call unbindService to my service and
call bindService in onStart event.
MyService work well after 3days. i let my android phone run during
3days to test myservice without turning my phone off.
But i got problem with myservice after 3days. myservice still run in
background. i knew this because i saw myservice in list of services
return from ActivityManager.getRunningServices(Integer.MAX_VALUE). But
the problem is that Activity A cannot bind to MyService anymore. i
cannot bind Activity A to MyService and i cannot pass data from
Activity to service as well.

Activity A can only bind to MySerice by calling startService(new
Intent(getBaseContext(), MyService.class)) again.
So what wrong with myservice? it's still running in background but
Activity A cannot bind to it. i dont 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.

Please, android developers are working on android core at google
please help me explain this and show me the way to solve this problem.
Thanks all you so much.



-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


[android-developers] Please help me out with problem of indefinite service

2011-11-11 Thread phannguyen
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);