【eoeAndroid精华推荐】谷歌语音识别 Demo 程序内自启动apk文件-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

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

2014-01-14 11:54:26 11023 1
谷歌语音识别 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社区-快乐冰茶的博客
  
认证移动开发者,两台云服务器免费拿



取消 提交回答
全部回答(1)
  • cn0555
    2014-01-14 15:20:16
    您的无私奉献精神值得我们学习!向您致敬!
    0 0
相关问答

1

回答

android viewpager禁止滑动

2016-06-07 10:49:07 1308浏览量 回答数 1

1

回答

Android 管理自启动程序

2016-06-20 08:26:45 1633浏览量 回答数 1

1

回答

Android 自定义相机录制视频出错 - 安卓报错

2020-05-30 11:19:41 242浏览量 回答数 1

1

回答

Android编程自动生成的r.java文件,里面的代码特别多

2016-06-01 15:04:57 1679浏览量 回答数 1

4

回答

android oss sdk ErrorMessage: divide by zero

2016-04-28 19:38:51 3991浏览量 回答数 4

1

回答

ANdroid webview和viewpage滑动事件冲突了

2016-03-16 14:17:45 2290浏览量 回答数 1

2

回答

id i-2zebopxrlqc8ydp5e5ht为什么不能再申请备案服务号?

2019-01-16 20:45:48 404浏览量 回答数 2

2

回答

请问android中为何子类重写oncreate方法之后,方法体还要用super调用同名方法

2016-03-16 11:05:30 2069浏览量 回答数 2

1

回答

对象存储OSS预售资源包套餐FP-nzu83f25b ,将在7天内到期

2018-08-27 08:32:54 1489浏览量 回答数 1

0

回答

咨询下各位大佬,flinkcdc采集Oracle,database-name取了SID,schema

2022-07-08 16:49:13 95浏览量 回答数 0
+关注
0
文章
20
问答
问答排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载