Android 快速连续多次启动意向服务是否会导致零额外费用?

Android 快速连续多次启动意向服务是否会导致零额外费用?,android,android-intent,Android,Android Intent,我们发现一个问题,其中一个intent服务意外地为一些用户检索了一个额外的null字符串。我们还无法重现这种情况,我们也不知道它在受影响用户的设备上是随机的还是一致的。受影响的用户和设备类型或安卓版本之间似乎没有关联 我正在扩展IntentService并实现handleIntent方法,如下所示: @Override public void handleIntent(Intent intent) { String action = intent.getAction(); if

我们发现一个问题,其中一个intent服务意外地为一些用户检索了一个额外的
null
字符串。我们还无法重现这种情况,我们也不知道它在受影响用户的设备上是随机的还是一致的。受影响的用户和设备类型或安卓版本之间似乎没有关联

我正在扩展
IntentService
并实现
handleIntent
方法,如下所示:

@Override
public void handleIntent(Intent intent) {
    String action = intent.getAction();

    if (action.Equals(ACTION_MARK_UNREAD)) {
        String messageKey = intent.getStringExtra(EXTRA_MESSAGE_KEY);

        // messageKey is null for some users
    }
}
带字段:

public static final String ACTION_MARK_UNREAD = "com.myapp.action.MARK_UNREAD";
public static final String EXTRA_MESSAGE_KEY = "extraMessageKey";
在一个片段中,我们快速连续启动此服务6次:

    for (int i = 0; i < 6; i++) {
            Intent i = new Intent(MyIntentService.ACTION_MARK_UNREAD);
            i = i.setClass(mContext, MyIntentService.class);
            i.putExtra(MyIntentService.EXTRA_MESSAGE_KEY, i.toString());
            mContext.startService(i);
    }
for(int i=0;i<6;i++){
意向i=新意向(MyIntentService.ACTION\u MARK\u UNREAD);
i=i.setClass(mContext,MyIntentService.class);
i、 putExtra(MyIntentService.EXTRA_MESSAGE_KEY,i.toString());
mContext.startService(i);
}
你知道为什么服务会为
messageKey
extra检索
null

我们在应用程序中有其他区域启动了相同的服务,但我们无法确定这种情况发生时它来自哪个区域。然而,从日志来看,它似乎来自我提到的这个片段。日志显示,
null
发生时的客户端时间戳是上次发生后的几秒钟。这可能是因为服务队列移动缓慢,或者我的假设可能是错误的。

您愿意试试吗

for (int y = 0; y < 6; y++) {
        Intent i = new Intent(MyIntentService.ACTION_MARK_UNREAD);
        i = i.setClass(mContext, MyIntentService.class);
        i.putExtra(MyIntentService.EXTRA_MESSAGE_KEY, y.toString());
        mContext.startService(i);
}
for(int y=0;y<6;y++){
意向i=新意向(MyIntentService.ACTION\u MARK\u UNREAD);
i=i.setClass(mContext,MyIntentService.class);
i、 putExtra(MyIntentService.EXTRA_MESSAGE_KEY,y.toString());
mContext.startService(i);
}

可能是编译器问题

我的第一个猜测是许多其他人评论过的编译器问题,我在您的示例中发现了一些其他错误,例如
Equals
,上面是E

但考虑到您的代码只是一个例子,我对这种方法进行了大量测试,我得出的第一个结论是,无论您的调用是否在
片段
活动
中,因为
片段
上下文
与父
活动
相同

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        for (int i = 0; i < 10; i++) {
            Intent intent = new Intent(MyIntentService.MY_ACTION);

            intent.setClass(this, MyIntentService.class);
            intent.putExtra(MyIntentService.EXTRA, UUID.randomUUID().toString());

            startService(intent);
        }
    }
}
阅读
IntentService
文档,我们可以看到:

所有请求都在单个工作线程上处理——它们可以作为 只要有必要(并且不会阻塞应用程序的主循环), 但一次只处理一个请求

在这里:

因此,我们可以得出结论,您的所有X请求都将逐个排队处理

Android文档中关于IntentService的另一部分我们可以看到:

因为大多数已启动的服务不需要处理多个请求 同时(这实际上可能是一个危险的多线程 场景),最好使用 IntentService类

在这里:

因此,有了这些信息,您可以认为
IntentService
是否是您所需要的方法。对吧?

在这里,您可以了解如何仅扩展
服务

完成后,我将在下面粘贴我为测试您的方法而编写的代码

活动
Main活动

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        for (int i = 0; i < 10; i++) {
            Intent intent = new Intent(MyIntentService.MY_ACTION);

            intent.setClass(this, MyIntentService.class);
            intent.putExtra(MyIntentService.EXTRA, UUID.randomUUID().toString());

            startService(intent);
        }
    }
}
以及输出
Log

my_extra﹕ b6faeb0a-29fa-442b-b87e-9c7a5f8c35d7
my_extra﹕ 88076250-d455-4084-af5f-c560ba6d5570
my_extra﹕ 21339466-25ab-4aaa-aadd-344555c4c2df
my_extra﹕ 2f935a93-465b-4648-a3cc-60f0c9cc67a4
my_extra﹕ 128653d1-d6af-499f-8725-78158e2e7190
my_extra﹕ e453ae7b-e21a-41fe-bf9c-f45ccfd13edf
my_extra﹕ 2e3fc6aa-e425-41dd-a584-8ab056fb906d
my_extra﹕ a8d90d53-c6cd-4d15-84f9-4064d6972de9
my_extra﹕ 721dd17b-b977-4029-ada3-5999f0eb36e7
my_extra﹕ e83d3277-adc8-47a8-a246-6cd7f6f2735d

对不起,我不知道是什么导致了你的问题。据我所知,多次启动这样的服务应该不是问题。您是否覆盖了IntentService的OnStart命令?真奇怪,您传递的是哪种上下文?您的代码显然大部分时间都有效,但我发现奇怪的是,您可以使用
int I
Intent I
而不会导致错误。正如@TTransmit指出的,您发布的代码在语法上是不正确的(变量
i
用于
int
Intent
,因此不会编译。请使用您拥有的实际代码更新您的帖子,这可能有助于解决您的问题。