首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android -如何从我的拼写检查服务获得一个会话?

Android -如何从我的拼写检查服务获得一个会话?
EN

Stack Overflow用户
提问于 2016-09-05 11:17:46
回答 1查看 2.2K关注 0票数 1

我正在尝试实现一个名为如前所述的拼写检查服务SampleSpellCheckerService,但该教程似乎不完整,而且它的源代码似乎不可用。

在活动的setSuggestionsFor()方法中,我很难从拼写检查器服务中获得会话,如下所示:

代码语言:javascript
复制
public class SpellCheckerSettingsActivity extends AppCompatActivity implements SpellCheckerSession.SpellCheckerSessionListener {

    private static final String LOG_TAG = SpellCheckerSettingsActivity.class.getSimpleName();

    private TextView textView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spell_checker_settings);

        final EditText editText = (EditText)findViewById(R.id.editText);

        textView = (TextView)findViewById(R.id.textView);

        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fetchSuggestionsFor(editText.getText().toString());
            }
        });

        startService(new Intent(this, SampleSpellCheckerService.class));

    }

    private void fetchSuggestionsFor(String input){

        Log.d(LOG_TAG, "fetchSuggestionsFor(\"" + input + "\")");

        /***************************************************
         * 
         * This line is invalid. What do I replace it with?
         * 
         ***************************************************/
        SpellCheckerSession session = SampleSpellCheckerService.getSession();

        TextInfo[] textInfos = new TextInfo[]{ new TextInfo(input) };
        int suggestionsLimit = 5;
        session.getSentenceSuggestions(textInfos, suggestionsLimit);

    }

    @Override
    public void onGetSuggestions(SuggestionsInfo[] results) {

        Log.d(LOG_TAG, "onGetSuggestions(" + results + ")");

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textView.setText("Suggestions obtained (TODO - get from results[])");
            }
        });

    }

    @Override
    public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {

        Log.d(LOG_TAG, "onGetSentenceSuggestions(" + results + ")");

        if (results != null) {
            final StringBuffer sb = new StringBuffer("");
            for (SentenceSuggestionsInfo result : results) {
                int n = result.getSuggestionsCount();
                for (int i = 0; i < n; i++) {
                    int m = result.getSuggestionsInfoAt(i).getSuggestionsCount();

                    for (int k = 0; k < m; k++) {
                        sb.append(result.getSuggestionsInfoAt(i).getSuggestionAt(k))
                                .append("\n");
                    }
                    sb.append("\n");
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    textView.setText(sb.toString());
                }
            });
        }

    }

    @Override
    public void onDestroy() {
        stopService(new Intent(this, SampleSpellCheckerService.class));
        super.onDestroy();
    }
}

那么,从SampleSpellCheckerService**?**获取会话的正确方法是什么?

为了完整起见,这里是我的拼写检查器服务类:

代码语言:javascript
复制
public class SampleSpellCheckerService extends SpellCheckerService {

    public static final String LOG_TAG = SampleSpellCheckerService.class.getSimpleName();

    public SampleSpellCheckerService() {
        Log.d(LOG_TAG, "SampleSpellCheckerService");
    }

    @Override
    public void onCreate() {
        super.onCreate();

        Log.d(LOG_TAG, "SampleSpellCheckerService.onCreate");
    }

    @Override
    public Session createSession() {

        Log.d(LOG_TAG, "createSession");

        return new AndroidSpellCheckerSession();
    }

    private static class AndroidSpellCheckerSession extends SpellCheckerService.Session {

        @Override
        public void onCreate() {

            Log.d(LOG_TAG, "AndroidSpellCheckerSession.onCreate");

        }



        @Override
        public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(TextInfo[] textInfos, int suggestionsLimit) {

            Log.d(LOG_TAG, "onGetSentenceSuggestionsMultiple");

            SentenceSuggestionsInfo[] suggestionsInfos = null;
            //suggestionsInfo = new SuggestionsInfo();
            //... // look up suggestions for TextInfo
            return suggestionsInfos;
        }

        @Override
        public SuggestionsInfo onGetSuggestions(TextInfo textInfo, int suggestionsLimit) {

            Log.d(LOG_TAG, "onGetSuggestions");

            SuggestionsInfo suggestionsInfo = null;
            //suggestionsInfo = new SuggestionsInfo();
            //... // look up suggestions for TextInfo
            return suggestionsInfo;
        }

        @Override
        public void onCancel() {
            Log.d(LOG_TAG, "onCancel");
        }


    }
}

这是我的舱单:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example">

    <permission android:name="android.permission.BIND_TEXT_SERVICE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <service
            android:name="com.example.SampleSpellCheckerService"
            android:label="@string/app_name"
            android:enabled="true"
            android:permission="android.permission.BIND_TEXT_SERVICE">
            <intent-filter>
                <action android:name="android.service.textservice.SpellCheckerService" />
            </intent-filter>

            <meta-data
                android:name="android.view.textservice.scs"
                android:resource="@xml/spellchecker" />
        </service>

        <activity android:name="com.example.SpellCheckerSettingsActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是我的spellchecker.xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<spell-checker
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/spellchecker_name"
    android:settingsActivity="com.example.SpellCheckerSettingsActivity">
    <subtype
        android:label="@string/subtype_generic"
        android:subtypeLocale="en" />
    />
    <subtype
        android:label="@string/subtype_generic"
        android:subtypeLocale="en_GB" />
    />
</spell-checker>

注:我正在用三星设备进行测试。

EN

回答 1

Stack Overflow用户

发布于 2016-12-16 04:01:01

据我从文档和一些示例代码中可以看到,似乎存在一些对android拼写检查API的误解,这导致了您的错误。

据我所知,您不能直接调用您的服务,因为API的目标是为您定义一个拼写检查器,用户必须首先从系统设置中选择它。基本上,您将设置活动(为与服务相关的设置显示的)与服务的测试活动混为一谈。

一些更好的教程是用android博客这里编写的,在github上镜像的android示例之间可以找到测试客户端基本示例服务的一些示例代码。

到目前为止,您得到的是示例服务(虽然链接的示例提供了更多的代码来查看如何实现这些方法),但是您有了区域设置定义所需的spellchecker.xml和在设置中出现的拼写检查程序名称,您已经有了一个设置活动(正如在spellchecker.xml中定义的,但只要您不需要任何首选项就不需要),并且您有一个实现SpellCheckerSessionListener的活动(尽管您将其命名为设置活动)。

您仍然需要做的是,转到您的settings -> Language & keyboard -> activate Spell checker并选择您的拼写检查器。

要从该拼写检查器获取会话,您可以使用

代码语言:javascript
复制
        final TextServicesManager tsm = (TextServicesManager) getSystemService(
            Context.TEXT_SERVICES_MANAGER_SERVICE);
    mScs = tsm.newSpellCheckerSession(null, null, this, true);

从样本中可以看到。

编辑:如果服务不需要任何设置,可以从xml中删除xml属性:

代码语言:javascript
复制
 android:settingsActivity="com.example.SpellCheckerSettingsActivity"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39329452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档