To monitor the state of a service, I suggest binding to it without the flag
to create it.  Then you will not cause it to start, but be told when it is
created or destroyed via the appropriate callbacks.

On Tue, Jan 6, 2009 at 1:24 AM, yukinoba <ckmagic...@gmail.com> wrote:

>
> to dear Dianne,
>
> I need to implement a remote background service and a service
> controller activity. I want to present an effect which the controller
> activity could know whether or not this service is already started,
> and enable / disable the menu button which is used to start / bind /
> unbind / stop the service.
>
> However, the controller activity may be paused, or destoryed. I have
> tried to write the status in a global variable, and with the activity
> destoryed, the variable value lost. I also tried to write into a
> SharedPreference, however, this will be persistent even the emulator
> shuts down. In the later case, the preference status will mislead the
> controller activity when the system is up next time.
>
> So, I need a way to know whether a specified background service is
> launched or not even if the controller activity is destroyed and back
> then. :-)
>
> Best regards,
> Nicholas
>
> On 1月6日, 下午3時40分, "Dianne Hackborn" <hack...@android.com> wrote:
> > 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 <ckmagic...@gmail.com> 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 <ckmagic...@gmail.com> wrote:
> > > > Ummm... does anyone know? have any ideas?
> >
> > > > On 1月5日, 下午12時05分, yukinoba <ckmagic...@gmail.com> 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
> > hack...@android.com
> >
> > 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.
> >
>


-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

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

Reply via email to