难道startService()利用现有的创建一个新的服务实例或? [英] Does startService() create a new Service instance or using the existing one?

查看:128
本文介绍了难道startService()利用现有的创建一个新的服务实例或?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

难道startService()创建一个新的服务实例或使用现有的? 例如,在下面的code,它创建两个UpdateService情况下,或只是一个UpdateService实例? 谢谢你。

  INT [] appWidgetIds =新INT [] {1,2};
    对于(INT appWidgetId:appWidgetIds){
        意向意图=新的意图(背景下,UpdateService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId);
        context.startService(意向);
    }
 

解决方案

如果已启动该服务,也不能正常启动第二个副本,但 ONSTART()是还是叫上现有的实例。服务是天然的单身 - 恰好有操作该服务的0或1份

Does startService() create a new Service instance or using the existing one? For example, in the following code, does it create two UpdateService instances or just one UpdateService instance? Thanks.

    int[] appWidgetIds = new int[] {1, 2};  
    for (int appWidgetId : appWidgetIds) {
        Intent intent = new Intent(context, UpdateService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        context.startService(intent);
    }

解决方案

If the service is already started, it does not start as second copy, but onStart() is still called on the existing instance. Services are natural singletons -- there is exactly 0 or 1 copy of the service in operation.

这篇关于难道startService()利用现有的创建一个新的服务实例或?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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