开发者

Getting the first result on Google Speech recognition

In the Android dev guide there is a开发者_高级运维n example on how to implement the speech recognition. That example prints a list of results using an array.

What if I'm only interested in the first result?

I have implemented it this way (data is the Intent returned by the Activity result):

data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS).get(0)

Is there another way to get just the first result without getting an array? Something like a getFirst method?


No, the api does not specify some kind of getFirst method.

If you use this in different places, you can create one for yourself, which does a null check and returns the first result:

public String getFirst(Intent data){
    List<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
    if(results != null && results.size() > 0){
        return results.get(0);
    }
    return null; //or maybe: return "";
}


The object returned from getStringArrayListExtra() is a ArrayList, which does not offer a getFirst() call. You could call iterator() and get the first entry on that via next(), but get(0) will probably be more efficient, because it is a plain array access in contrast to an object creation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜