I would strongly recommend against that, it can be quite inefficient.  This
API is really intended for applications to display a list of running
services.

If your service is running in the same process as the code wanting to check
its state, you can just set a global variable while it is running.

Why do you need to do this?

On Mon, Jan 5, 2009 at 10:34 PM, yukinoba <[email protected]> wrote:

>
> Hi all,
>
> I have already found out how to get this done. Please refer to the
> following code:
>
>    /**
>     * Indicates whether the specified service is already started.
> This
>     * method queries the activity manager for launched services that
> can
>     * respond to an binding with an specific service name.
>     * If no existed service is found, this method returns null.
>     *
>     * @param context The context of the activity
>     * @param className The service full name to check for
> availability.
>     *
>     * @return ComponentName if the service is already existed, NULL
> otherwise.
>     */
>    public static ComponentName isServiceExisted(Context context,
> String className)
>    {
>        ActivityManager activityManager =
>                (ActivityManager)
> context.getSystemService(ACTIVITY_SERVICE);
>
>        List<ActivityManager.RunningServiceInfo> serviceList
>        = activityManager.getRunningServices(Integer.MAX_VALUE);
>
>        if(!(serviceList.size() > 0))
>        {
>                return null;
>        }
>
>        for(int i = 0; i < serviceList.size(); i++)
>        {
>                RunningServiceInfo serviceInfo = serviceList.get(i);
>                ComponentName serviceName = serviceInfo.service;
>
>                if(serviceName.getClassName().equals(className))
>                {
>                        return serviceName;
>                }
>        }
>
>        return null;
>    }
>
> Best regards,
> Nicholas
>
> On 1月6日, 上午10時58分, yukinoba <[email protected]> wrote:
> > Ummm... does anyone know? have any ideas?
> >
> > On 1月5日, 下午12時05分, yukinoba <[email protected]> wrote:
> >
> > > To dear all Android developers,
> >
> > > Is there a way to check whether a service is already started or not?
> > > This issue has been discussed ealier (and much earlier) in April, in
> > > the last year, on this forum.
> >
> > >http://groups.google.com/group/android-developers/browse_thread/threa.
> ..
> >
> > > However, I wonder that does there have a new solution to this issue
> > > rather than using a SharedPreferences to store the service status. Is
> > > this functionality to check service is started or not has been added
> > > to SDK1.0_r2 or Cupcake branch?
> >
> > > Thanks for any help or solutions! :-)
> >
> > > Best regards,
> > > Nicholas
> >
>


-- 
Dianne Hackborn
Android framework engineer
[email protected]

Note: please don't send private questions to me, as I don't have time to
provide private support.  All such questions should be posted on public
forums, where I and others can see and answer them.

--~--~---------~--~----~------------~-------~--~----~
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