1

SampleSpellCheckerService이라는 맞춤법 검사기 서비스 as described here을 구현하려고하지만 튜토리얼이 불완전하고 소스 코드가 사용 가능한 것으로 보이지 않습니다.Android - 맞춤법 검사기 서비스에서 세션을받는 방법?

여기 강조 나는, 내 활동의 setSuggestionsFor() 방법에 내 맞춤법 검사 서비스에서 세션을 얻는 방법으로 고민하고

:

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에서 세션을 얻을 수있는 올바른 방법은 무엇입니까 ? 여기

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"); } } } 

내 매니페스트입니다 :

<?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입니다 :

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

완성도를 들어

, 여기에 내 맞춤법 검사기 서비스 클래스 NB - 삼성 기기로 테스트 중입니다.

답변

0

지금까지 내가 문서 및 일부 샘플 코드에서 볼 수있는 것처럼 안드로이드 맞춤법 검사 API에 대한 오해가있는 것으로 보입니다. 그 결과로 오류가 발생합니다.

API의 목표는 사용자가 먼저 시스템 설정에서 선택해야하는 맞춤법 검사기를 정의하기위한 것이므로 직접 서비스를 호출 할 수 없다고 말할 수 있습니다. 기본적으로 서비스 관련 설정에 대해 표시되는 설정 활동과 서비스에 대한 테스트 활동이 섞여 있습니다.

더 좋은 자습서는 testing clientandroid dev bloghere, 일부 샘플 코드로 작성하고 rudimentary example service은 GitHub의에 미러 안드로이드 샘플 사이에 볼 수 있습니다.

샘플 서비스는 링크 된 샘플이 구현할 수있는 방법을 확인하는 데 더 많은 코드를 제공하지만 사용자가 spellchecker.xml에 로캘 정의 및 설정에 나타나는 맞춤법 검사기 이름이 필요합니다. , 이미 설정 활동 (spellchecker.xml에 정의 된대로, 환경 설정이 필요하지 않는 한 필요 없음) 및 SpellCheckerSessionListener을 구현하는 활동이 있습니다 (설정 활동으로 이름을 지정 했음).

아직도해야 할 일은 settings ->Language & keyboard ->Spell checker을 활성화하고 맞춤법 검사기를 선택하십시오.

은 샘플에서 볼 수 있듯이 당신이 다음

 final TextServicesManager tsm = (TextServicesManager) getSystemService(
      Context.TEXT_SERVICES_MANAGER_SERVICE); 
    mScs = tsm.newSpellCheckerSession(null, null, this, true); 

으로 API를 호출 할 수있는 맞춤법 검사기에서 세션을 얻으려면.

편집 :

android:settingsActivity="com.example.SpellCheckerSettingsActivity" 
+0

감사합니다 : 당신이 당신의 서비스에 대한 설정을하지 않아도 경우, 당신은 당신의 XML에서 XML 속성을 제거 할 수 있습니다. 사용자가 설정을 어지럽히 게하는 것이 나를위한 실행 가능한 솔루션이 아닙니다. SearchView에서 내 맞춤법 검사기를 구현하는 더 좋은 방법을 추천 해 주시겠습니까?사용자가 제품 이름을 잘못 입력하면 (예 : * Badli Spelled Product *) 실제 제품 (예 : my * Badly Spelled * 제품)이 제공됩니다. 처음부터이 기능을 작성해야합니까? (아니면 내가 사용할 수있는 무료, 신뢰할 수있는 웹 기반 API가 있습니까?) –

+0

IMHO : 전체 맞춤법 검사 API는 일종의 단계 자식입니다. HTC에서는 Google 키보드 나 htc 감각 모두 사용자 정의 맞춤법 검사기를 사용하지 않지만 둘 다 자체적으로 사용합니다. 맞춤법 검사기를 사용하려는 다른 응용 프로그램은 특정 인터페이스를 구현해야하므로 사용자에게 쓸모가 없습니다. 응용 프로그램 특정 맞춤법 검사 (당신이 원했던 것처럼) 전체 맞춤법 검사 API를 버리고 텍스트 워처와 함께 가자. –

+0

고마워.하지만 어떻게 TextWatcher가 맞춤법 검사를 도와 줄거야? –