Voice Recognition Service : Service « Network « Android






Voice Recognition Service

  
//
//src\com\example\android\voicerecognitionservice\VoiceRecognitionService.java
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.voicerecognitionservice;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.RemoteException;
import android.speech.SpeechRecognizer;
import android.speech.RecognitionService;

/**
 * A sample implementation of a {@link RecognitionService}. This very simple implementation does
 * no actual voice recognition. It just immediately returns fake recognition results.
 * Depending on the setting chosen in {@link VoiceRecognitionSettings}, it either returns a
 * list of letters ("a", "b", "c"), or a list of numbers ("1", "2", "3").
 */
public class VoiceRecognitionService extends RecognitionService {

    @Override
    protected void onCancel(Callback listener) {
        // A real recognizer would do something to shut down recognition here.
    }

    @Override
    protected void onStartListening(Intent recognizerIntent, Callback listener) {
        // A real recognizer would probably utilize a lot of the other listener callback
        // methods. But we'll just skip all that and pretend we've got a result.
        ArrayList<String> results = new ArrayList<String>();
        
        SharedPreferences prefs = getSharedPreferences(
                VoiceRecognitionSettings.SHARED_PREFERENCES_NAME,
                Context.MODE_PRIVATE);
        
        String resultType = prefs.getString(
                VoiceRecognitionSettings.PREF_KEY_RESULTS_TYPE,
                String.valueOf(VoiceRecognitionSettings.RESULT_TYPE_LETTERS));
        int resultTypeInt = Integer.parseInt(resultType);
        
        if (resultTypeInt == VoiceRecognitionSettings.RESULT_TYPE_LETTERS) {
            results.add("a");
            results.add("b");
            results.add("c");            
        } else if (resultTypeInt == VoiceRecognitionSettings.RESULT_TYPE_NUMBERS) {
            results.add("1");
            results.add("2");
            results.add("3");
        }
        
        Bundle bundle = new Bundle();
        bundle.putStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION, results);
        
        try {
            listener.results(bundle);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void onStopListening(Callback listener) {
        // Not implemented - in this sample we assume recognition would be endpointed
        // automatically, though certain applications may wish to expose an affordance
        // for stopping recording manually.
    }
    
}



//src\com\example\android\voicerecognitionservice\VoiceRecognitionSettings.java
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.example.android.voicerecognitionservice;

import android.os.Bundle;
import android.preference.PreferenceActivity;

/**
 * A settings activity for the sample voice recognizer.
 */
public class VoiceRecognitionSettings extends PreferenceActivity {
    
    // The name of the SharedPreferences file we'll store preferences in.
    public static final String SHARED_PREFERENCES_NAME = "VoiceRecognitionService";
    
    // The key to the preference for the type of results to show (letters or numbers).
    // Identical to the value specified in res/values/strings.xml.
    public static final String PREF_KEY_RESULTS_TYPE = "results_type";
    
    // The values of the preferences for the type of results to show (letters or numbers).
    // Identical to the values specified in res/values/strings.xml.
    public static final int RESULT_TYPE_LETTERS = 0;
    public static final int RESULT_TYPE_NUMBERS = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPreferenceManager().setSharedPreferencesName(SHARED_PREFERENCES_NAME);
        addPreferencesFromResource(R.xml.preferences);
    }
}



//
//res\values\strings.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
/*
** Copyright 2010, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    
    <!-- The name for the recognizer - to be shown in system settings. -->
    <string name="service_name">Sample Recognizer</string>
        
    <!-- The name of the settings activity. -->
    <string name="settings_name">Voice Recognition Settings</string>
    
    <!-- The title of the preference for the type of results to return (letters or numbers). -->
    <string name="results_type_title">Results type</string>
    
    <!-- The entry names of the preference for the type of results to return
         (letters or numbers). -->
    <string-array name="results_type_entries">
        <item>Letters</item>
        <item>Numbers</item>
    </string-array>
    
    <!-- The entry values of the preference for the type of results to return
         (letters or numbers). -->
    <string-array name="results_type_values">
        <item>0</item>
        <item>1</item>
    </string-array>
    
    <!-- The default value of the preference for the type of results to return
         (letters or numbers). -->
    <string name="results_type_default_value">0</string>

</resources>



//
//res\xml\preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010, The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
        android:title="@string/settings_name">

    <!-- This setting lets you choose what kind of fake results you get from
         this sample recognizer. If you choose "letters", you'll get letters
         like "a", "b", "c" in the list of results. If you choose "numbers",
         you'll get numbers like "1", "2", "3" in the list of results. -->
    <ListPreference
            android:key="results_type"
            android:title="@string/results_type_title"
            android:entries="@array/results_type_entries"
            android:entryValues="@array/results_type_values"
            android:dialogTitle="@string/results_type_title"
            android:defaultValue="@string/results_type_default_value"
    />

</PreferenceScreen>



//res\xml\recognizer.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
/**
 * Copyright (c) 2010, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
-->

<!-- The attributes in this XML file provide configuration information
     for the sample voice recognizer. -->

<recognition-service xmlns:android="http://schemas.android.com/apk/res/android"
        android:settingsActivity="com.example.android.voicerecognitionservice.VoiceRecognitionSettings"
/>

   
    
  








Related examples in the same category

1.Map Service
2.Widget Service
3.Restful web service task
4.Search with Restful service
5.Post Restful service
6.Tracker Service
7.Weather web service
8.Access a Web service using GET
9.Alarm service
10.Get Running Services Info
11.Local service demo
12.extends Service
13.Service structure
14.Clock Back Service
15.is Connected by Context.CONNECTIVITY_SERVICE
16.BeatService extends Service and Thread
17.TimeoutService (beta). Here you can register a timeout.
18.Timeout Service
19.Screenshot Service