[英]Call service method from activity
I need to call a function from a service when a item from the options menu is pressed: 当按下选项菜单中的项目时,我需要从服务中调用函数:
if (id == R.id.action_connect) {
if (mIsBound) {
LocalService.connect();
// cannot be referenced from a static context
}
}
I'm using these docs . 我正在使用这些文档 。 I read a few answers but I'm not sure about this one. 我读了一些答案,但是我不确定。
You can call startService(someIntent); 您可以调用startService(someIntent);
For example from an activity you can do something like this 例如,从一个活动中,您可以执行以下操作
Intent serviceIntent = new Intent(this, TheService.class);
serviceIntent.addCategory("some_unique_string");
startService(serviceIntent);
Then in the service 然后在服务
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
if (intent.hasCategory("some_unique_string")) {
theMethodYouWantToCall();
} else if (intent.hasCategory("some_other_string")) {
someOtherMethod();
}
}
return START_STICKY;
}
You can call startService as often as you like. 您可以根据需要多次调用startService。
The basic idea is to create an intent that represents the "intended" method you want to call , then in the service onStartCommand method, figure out what method you should call based on the information you pass in via the intent and then call the method. 基本思想是创建一个表示要调用的“预定”方法的意图,然后在服务onStartCommand方法中,根据通过该意图传递的信息,确定应调用的方法,然后调用该方法。
Note: You must check that the intent is not null. 注意:您必须检查意图不为空。 If the system ever kills and restarts your service, it will do so effectively calling startService with a null for the intent, so if you leave that part out you will at some point get NPE crash. 如果系统终止并重新启动服务,它将有效地调用startService并使用null表示意图,因此,如果将该部分遗漏, 则有时会导致NPE崩溃。
i don't have reputation to comment. 我没有评论的声誉。 but it is better to set an action instead of category. 但是最好设置一个动作而不是类别。 like 喜欢
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String action = intent.getAction();
if (action.equals(ACTION_PLAY))
processPlayRequest();
else if (action.equals(ACTION_PAUSE))
processPauseRequest();
else if (action.equals(ACTION_SKIP))
processSkipRequest();
else if (action.equals(ACTION_STOP))
processStopRequest();
else if (action.equals(ACTION_REWIND))
processRewindRequest();
else if (action.equals(ACTION_URL))
processAddRequest(intent);
return START_NOT_STICKY; // Means we started the service, but don't want
// it to
// restart in case it's killed.
}
And also add them in manifest like 并在清单中添加它们,例如
<service
android:exported="false"
android:name="com.cibotechnology.KXNT.MusicService"
>
<intent-filter>
<action
android:name="com.cibotechnology.KXNT.action.PLAY" />
<action
android:name="com.cibotechnology.KXNT.action.PAUSE" />
<action
android:name="com.cibotechnology.KXNT.action.SKIP" />
<action
android:name="com.cibotechnology.KXNT.action.REWIND" />
<action
android:name="com.cibotechnology.KXNT.action.STOP" />
</intent-filter>
<intent-filter>
<action
android:name="com.cibotechnology.KXNT.action.URL" />
<data
android:scheme="http" />
</intent-filter>
</service>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.