安卓:在服务中检索一个额外的意图是什么? [英] Android: retrieving an extra from intent in service?

查看:118
本文介绍了安卓:在服务中检索一个额外的意图是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我开始,我想沿着传递 putExtra 整数值的服务:

So I'm starting a service with a putExtra integer value that I want to pass along:

        Intent i = new Intent(context, clickService.class); 
        i.putExtra("numClicks", thisNumClicks); 
        context.startService(i);

和这将启动该服务。现在的问题是我怎么检索此值回,而在服务?下面是我想,我记得使用在previous项目(从活动中):

And this starts the service. The question is how do I retrieve this value back whilst in the service? Here's what I tried that I remember using in a previous project (from an activity):

int result = getIntent().getExtras().getInt("numClicks");

getIntent()不是为服务我猜界定?我怎么再使用?

However getIntent() is not defined for services I guess? What do I use then?

感谢。

推荐答案

从文档的<一个href=\"http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29\">Context.startService(Intent服务)

此方法每次调用都会导致对目标服务的onStartCommand(意向,INT,INT)方法,相应的调用与这里给出的意图。

Every call to this method will result in a corresponding call to the target service's onStartCommand(Intent, int, int) method, with the intent given here.

所以基本上只使用传递给 onStartCommand的意图(...)

So basically just use the Intent passed to onStartCommand(...).

这篇关于安卓:在服务中检索一个额外的意图是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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