开发者社区> 问答> 正文

【eoeAndroid精华推荐】谷歌语音识别 Demo 程序内自启动apk文件

谷歌语音识别 Demo 程序内自启动apk文件


此Demo 可以判断用户是否安装了谷歌的语音识别服务,如果没有安装,提示用户安装谷歌语音识别程序,程序将自启动谷歌语音识别程序。用户语音输入后,将返回的匹配度最高的呈现在一个TextView 中!注意申请SD卡权限

package com.example.soundres;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {
Button mBtnSound;
public static final int REQUEST_CODE_DOUND_REQ = 1;



@Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mBtnSound = (Button) findViewById(R.id.btnSound);
            mBtnSound.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                            try {
                                    Intent i = new Intent(
                                                    RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                                    startActivityForResult(i, REQUEST_CODE_DOUND_REQ);
                            } catch (Exception e) {
                                    new AlertDialog.Builder(MainActivity.this)
                                                    .setTitle("NotFound")
                                                    .setMessage("没有找到谷歌语音识别模块,是否安装?")
                                                    .setPositiveButton("是", new OnClickListener() {
                                                            @Override
                                                            public void onClick(DialogInterface dialog,
                                                                            int which) {
                                                                    FileOutputStream out = null;
                                                                    InputStream in = null;
                                                                    try {
                                                                            // 获取输入流
                                                                            in = getAssets()
                                                                                            .open("goolesearch.apk");
                                                                            // 获取本地文件夹目录
                                                                            File dir = Environment
                                                                                            .getExternalStorageDirectory();
                                                                            File file = new File(dir,
                                                                                            "/goolesearch.apk");
                                                                            out = new FileOutputStream(file);
                                                                            int len;
                                                                            byte[] buf = new byte[1024];
                                                                            while ((len = in.read(buf)) != -1) {
                                                                                    out.write(buf, 0, len);
                                                                            }
                                                                            Intent i = new Intent();
                                                                            // 设置启动模式
                                                                            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                                                            i.setAction(android.content.Intent.ACTION_VIEW);
                                                                            i.setDataAndType(Uri.fromFile(file),
                                                                                            "application/vnd.android.package-archive");
                                                                            startActivity(i);
                                                                    } catch (IOException e) {
                                                                            e.printStackTrace();
                                                                    } finally {
                                                                            try {
                                                                                    if (out != null) {
                                                                                            out.close();
                                                                                    }
                                                                                    if (in != null) {
                                                                                            in.close();
                                                                                    }
                                                                            } catch (IOException e) {
                                                                                    e.printStackTrace();
                                                                            }
                                                                    }
                                                            }
                                                    }).setNegativeButton("否", null).create().show();
                            }
                    }
            });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK) {
                    switch (requestCode) {
                    case REQUEST_CODE_DOUND_REQ:
                            ArrayList<String> speeks = null;
                            speeks = data
                                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                            // for (int i = 0; i < speeks.size(); i  ) {
                            // Log.i("speek", speeks.get(i));
                            // }
                            TextView mTv = (TextView) findViewById(R.id.tvSpeek);
                            mTv.setText(speeks.get(0));
                            break;
                    default:
                            break;
                    }
            }
            super.onActivityResult(requestCode, resultCode, data);


    }


本帖来源:eoe社区-快乐冰茶的博客
  
认证移动开发者,两台云服务器免费拿



展开
收起
笑傲江虎 2014-01-14 11:54:26 11345 0
1 条回答
写回答
取消 提交回答
  • 您的无私奉献精神值得我们学习!向您致敬!
    2014-01-14 15:20:16
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
阿里云总监课第二期——Neural Network Language Model在语音识别中的应用 立即下载
阿里云总监课第二期——Latency Controlled-BLSTM模型在语音识别中的应用 立即下载
智能语音交互:阿里巴巴的研究与实践 立即下载