You can return START_STICKY to make sure the service is always running 
unless the system is low in RAM memory. The system, in that case would 
restart your service when memory is available. Relying on ActivityManager 
is a dangerous thing to do as written below.

http://developer.android.com/reference/android/app/ActivityManager.html#getRunningServices(int)

Another way, you can put a string extra in the intent you use to start you 
Service and let the Service run as per what extra is supplied to it.

On Tuesday, 6 November 2012 04:32:41 UTC+5:30, [email protected] wrote:
>
> I have an activity and a service.  When the Activity first starts up I 
> create an Intent for the service and call StartService(i).  This properly 
> starts up the service triggering the services' onCreate event and 
> onStartCommand events.
>  
> Subsequent to that there are times when I want to pass the service some 
> information so, using the same Intent I add some data to a bundle using 
> i.putextra and call StartService(i) again.  To my surprise the onCreate 
> event of the service executes again!!  I would expect that only the 
> onStartCommand would execute on subsequent StartService(i) calls.  The 
> service was indeed still running when the call was made.
>  
> Is my expectation correct?  Why is onCreate executing again in the service?
>  
> //this is code from the Activity...
> public void onCreate(Bundle savedInstanceState) {
> .
> .
> .
> i = new Intent(this, StalkService.class);
> if (isMyServiceRunning())
> {
>  i.putExtra("mode", "feedmethewholetrip");
>  this.startService(i);
> }
> else
> {
>  i.putExtra("MESSENGER", messenger);
>  this.startService(i);
> }
>
>    private boolean isMyServiceRunning() {
>      String sClassName;
>         ActivityManager manager = (ActivityManager) 
> getSystemService(Context.ACTIVITY_SERVICE);
>         for (RunningServiceInfo service : 
> manager.getRunningServices(Integer.MAX_VALUE)) 
>         {
>             sClassName = service.service.getClassName();
>             Log.w(getClass().getName(), "Services are : " + sClassName);
>             if (sClassName.contains("com.deanblakely.StalkService")) {
>                 return true;
>             }
>         }
>         return false;
>     }
>

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

Reply via email to