一个Android服务的prevent多个副本 [英] Prevent multiple copies of an Android service

查看:115
本文介绍了一个Android服务的prevent多个副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的<一个href="http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29"相对=nofollow>多科以startService指出:如果该服务尚未运行,它将被实例化和启动(如果需要创建它的过程);如果是运行,那么它仍运行 我发现,每次调用startService似乎开始单独的服务的实例,在该服务正在做的工作(在我的测试情况下,平凡写入新的日志文件)正在再次为每个调用完成。 我试图通过ActivityManager ... getRunningServices(Integer.MAX_VALUE的))循环检测服务,但它没有显示出来。 在SGS 11的Andr​​oid 2.3.3 我失去了一些东西在这里。我理解的是,服务的onCreate(),它的创建方法时只被调用,而且因为我有一个持续的过程,在服务(运行

The doco for startService states "If this service is not already running, it will be instantiated and started (creating a process for it if needed); if it is running then it remains running." I'm finding that each call to startService appears to be starting a separate instance of the service, in that the work that the service is doing (in my test case, trivially writing to a new log file) is being done again for each call. I'm trying to detect the service by looping through ActivityManager... getRunningServices(Integer.MAX_VALUE)) but it's not showing up. Android 2.3.3 on SGS 11 I'm missing something here. I understood that the Service's onCreate() method only gets called when it's created, and that since I have a continuous process running in the Service (the

在我活动的onResume方法,我开始了服务(为myService)其中:

In my Activity's onResume method I'm starting the service ("myService")with:

    Intent intent = new Intent(this, myService.class);
startService(intent);

在为MyService我有一个像

In MyService I have an onCreate like

@Override 

public void onCreate(){
    super.onCreate();
    ...

这是我的地方设置一个计时器,使用TimerTask的,它写入日志文件一次/秒。

from where I set a timer, using a TimerTask, which writes to a log file once/second.

这工作正常 - 我可以看到被写入日志。 在活动的onResume方法,前调用StartService方法,我检查,看是否存在通过调用包含checkForRunningService方法为myService服务

This works as expected - I can see the log being written. In the Activity's onResume method, before calling the StartService method, I'm checking to see if the myService service exists by calling a checkForRunningService method containing

for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
    if (service.service.getClassName().contentEquals("com.gpsanimator.startrax.myService")) {
    Toast.makeText(getApplicationContext(), "Service IS SO running: ", Toast.LENGTH_SHORT).show();
        return true;
}

这从来没有找到为myService服务,即使它看上去正在运行,因为它是愉快地写入日志文件。

This never finds the myService service, even though it appears to be running as it's happily writing to the log file.

Eeach时间我恢复的活动,我为myService服务的onCreate方法被调用 - 它生成一个新的日志文件,并开始写它,以及原来的日志文件中被不断更新。 不服务开始第一次startService叫?然后继续运行?如果不是随后调用startService()找到服务已经在运行,因此不会再触发的onCreate()方法?但是,这不是我所看到的。似乎每次调用startService()被触发服务的onCreate()方法。

Eeach time I resume my Activity, the onCreate method of my myService service gets called - it generates a new log file and starts writing to it as well as the original log file being continuously updated. Doesn't the Service get started the first time the startService is called? And then continue running? Shouldn't subsequent calls to startService() find the Service already running and therefore not trigger the onCreate() method again? But that's not what I'm seeing. It seems that every call to startService() is triggering the Service's onCreate() method.

这显然比这更复杂,我想获得到它的底部。

It's obviously more complicated than this, and I would like to get to the bottom of it.

推荐答案

这个问题是我宣布我为myService类来扩展IntentService,没有服务! 有一次,我固定的,它的所有工作按照书!

The problem was I had declared my myService class to extend IntentService, not Service! Once I fixed that, it all worked as per the book!

这篇关于一个Android服务的prevent多个副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆