1、判断是否已安装语音搜索功能
package com.voice.search; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.net.Uri; import android.os.Environment; import android.speech.RecognizerIntent; import android.widget.Toast; public class VoiceSearchUtil { private static Context context; private static final int VOICE_RECOGNITION_REQUEST_CODE = 1; public static void getVoiceSearch(Context c) { context = c; PackageManager pm = context.getPackageManager(); List<ResolveInfo> activities = pm .queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); if (activities.size() != 0) { /* 设备存在 */ voiceSearch(); } else { /* 没有设备请点击安装按钮进行安装呢 */ if (copyApkFromAssets(context, "google_voice_search.apk", Environment.getExternalStorageDirectory() .getAbsolutePath() + "/google_voice_search.apk")) { new AlertDialog.Builder(context).setMessage("检测到未安装语音搜索设备,是否安装?") .setPositiveButton("安装", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType( Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/google_voice_search.apk"), "application/vnd.android.package-archive"); context.startActivity(intent); } }).setNegativeButton("取消", null).show(); } } } /** * 安装语音搜索功能,写入数据 * * @param context * 上下文 * @param fileName * 文件名称 * @param path * 文件路径 * @return 所需文件 */ public static boolean copyApkFromAssets(Context context, String fileName, String path) { boolean copyIsFinish = false; try { InputStream is = context.getAssets().open(fileName); File file = new File(path); file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); byte[] temp = new byte[1024]; int i = 0; while ((i = is.read(temp)) > 0) { fos.write(temp, 0, i); } fos.close(); is.close(); copyIsFinish = true; } catch (IOException e) { e.printStackTrace(); } return copyIsFinish; } // 语音搜索 private static void voiceSearch() { try { // 通过Intent传递语音识别的模式,开启语音 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); // 语言模式和自由模式的语音识别 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); // 提示语音开始 intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "开始语音"); // 开始语音识别 ((Activity) context).startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); } catch (Exception e) { e.printStackTrace(); Toast.makeText(context, "找不到语音设备", Toast.LENGTH_LONG).show(); } } }
2、assets提供语音搜索apk安装
package com.voice.search; import java.util.ArrayList; import android.os.Bundle; import android.speech.RecognizerIntent; import android.view.View; import android.widget.Toast; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; public class MainActivity extends Activity { private static final int VOICE_RECOGNITION_REQUEST_CODE = 1; private String[] str; private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; } public void onClick(View v) { switch (v.getId()) { case R.id.search_iv: VoiceSearchUtil.getVoiceSearch(context); break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // 回调获取从谷歌得到的数据 if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) { // 取得语音的字符 ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); // 循环所有结果存入数组 str = new String[results.size()]; for (int i = 0; i < results.size(); i++) { // 循环所有结果 str[i] = results.get(i); } // Dialog显示 getSearchDialog(); // 这里只获取第一条数据 Toast.makeText(this, results.get(0), Toast.LENGTH_LONG).show(); } super.onActivityResult(requestCode, resultCode, data); } /*** * 搜索后dialog显示结果 */ private void getSearchDialog() { Dialog dialog = new AlertDialog.Builder(this).setTitle("你是不是想找:").setIcon(R.drawable.ic_launcher) .setItems(str, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, str[which], Toast.LENGTH_SHORT).show(); } }).setNegativeButton("取消", null).create(); dialog.show(); } }
3、加入Android读写文件权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />