Android 从活动调用服务方法

Android 从活动调用服务方法,android,Android,当按下options(选项)菜单中的某一项时,我需要从服务调用一个函数: if (id == R.id.action_connect) { if (mIsBound) { LocalService.connect(); // cannot be referenced from a static context } } 我正在用这些。我读了一些答案,但我不确定这个答案。你可以打电话给startService(someIntent) 例如,在一项活动

当按下options(选项)菜单中的某一项时,我需要从服务调用一个函数:

if (id == R.id.action_connect) {
    if (mIsBound) {
        LocalService.connect();
        // cannot be referenced from a static context
    }
}
我正在用这些。我读了一些答案,但我不确定这个答案。

你可以打电话给startService(someIntent)

例如,在一项活动中,你可以做这样的事情

Intent serviceIntent = new Intent(this, TheService.class);
serviceIntent.addCategory("some_unique_string");
startService(serviceIntent);
然后在服役

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;
}
您可以随时调用startService

基本思想是创建一个表示要调用的“预期”方法的意图,然后在ServiceOnStartCommand方法中,根据通过意图传递的信息确定应该调用的方法,然后调用该方法


注意:您必须检查意图是否为空。如果系统终止并重新启动您的服务,它将有效地调用startService,并使用空值表示意图,因此如果您不使用该部分,将在某个时候导致NPE崩溃。

我没有评论的声誉。但最好设置一个操作而不是类别。像

@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.
    }
并将它们添加到清单中,如

<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>


我不知道你可以调用startService,即使它已经开始发送命令,谢谢!谢谢,非常感谢。