[AndroidTips]如何启动Voice Input或者Output引擎的设置界面

对于启动Voice Output的设置界面,参考下面的链接:

http://stackoverflow.com/questions/5702790/how-show-voice-input-and-output-settings-page-from-application

http://stackoverflow.com/questions/3160447/how-to-show-up-the-settings-for-text-to-speech-in-my-app

 

对于启动Voice Input的设置界面,我参考VoiceInputOutputSettings.java写了下面的代码:

    public void startSettings() {
        List<ResolveInfo> availableRecognitionServices = mContext.getPackageManager().queryIntentServices(
                new Intent(RecognitionService.SERVICE_INTERFACE),
                PackageManager.GET_META_DATA);
        int numAvailable = availableRecognitionServices.size();
        if (numAvailable < 1) {
            return;
        }

        String currentSetting = Settings.Secure.getString(mContext.getContentResolver(),
                "voice_recognition_service");
        ResolveInfo currentRecognizer = null;
        for (ResolveInfo resolveInfo : availableRecognitionServices) {
            String recognizerComponent = new ComponentName(resolveInfo.serviceInfo.packageName,
                    resolveInfo.serviceInfo.name).flattenToShortString();
            if (recognizerComponent.equals(currentSetting)) {
                currentRecognizer = resolveInfo;
                break;
            }
        }

        if (currentRecognizer == null) {
            return;
        }

        XmlResourceParser parser = null;
        String settingsActivity = null;
        ServiceInfo si = currentRecognizer.serviceInfo;
        try {
            parser = si.loadXmlMetaData(mContext.getPackageManager(),
                    RecognitionService.SERVICE_META_DATA);
            if (parser == null) {
                throw new XmlPullParserException("No " + RecognitionService.SERVICE_META_DATA +
                        " meta-data for " + si.packageName);
            }
            int type;
            while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
                    && type != XmlPullParser.START_TAG) {
            }

            String nodeName = parser.getName();
            if (!"recognition-service".equals(nodeName)) {
                throw new XmlPullParserException(
                        "Meta-data does not start with recognition-service tag");
            }
            int count = parser.getAttributeCount();
            for (int i = 0; i < count; i++) {
                String attrName = parser.getAttributeName(i);
                String attrValue = parser.getAttributeValue(i);
                if (attrName.equals("settingsActivity")) {
                    settingsActivity = attrValue;
                    break;
                }
            }
        } catch (XmlPullParserException e) {
            Log.e(TAG, "error parsing recognition service meta-data", e);
        } catch (IOException e) {
            Log.e(TAG, "error parsing recognition service meta-data", e);
        } finally {
            if (parser != null) {
                parser.close();
            }
        }

        if (settingsActivity != null) {
            try {
                Intent i = new Intent(Intent.ACTION_MAIN);
                i.setComponent(new ComponentName(si.packageName, settingsActivity));
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(i);
            } catch (ActivityNotFoundException e) {
                Log.e(TAG, "Error startActivity", e);
            }
        }
    }
 

你可能感兴趣的:(android)