android程式設計實例入門 -Sample改寫分享 (2014/10/29)

android程式設計實例入門 -Sample改寫分享 (2014/10/29)

android程式設計實例入門 -Sample改寫分享 (2014/10/29)

 

此範例為程式碼(08\Sample03)的改寫,利用一個LinearLayout一個Button和一個TextView來實作GUI界面,利用Intent呼叫RecognizerIntent來進行聲音辨識功能,而且將聲音辨識功能的結果透過onActivityResult將聲音辨識結果顯示出來,程式碼如下所示:

 

 

package com.jashsample;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.hardware.Camera.Size;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.text.format.DateFormat;
import android.text.method.KeyListener;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
/*
 * 建立Button*1+TextView*1,利用Intent呼叫RecognizerIntent來進行聲音辨識功能,而且將聲音辨��功能的結果透過onActivityResult將聲音辨��結果顯示出來。
 */
public class MainActivity extends Activity {
public static int SAMPLE_APP = 1;
Button bt;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
setContentView(ll);
tv = new TextView(this);
bt = new Button(this);
bt.setText("輸入");
ll.addView(tv);
ll.addView(bt);
bt.setOnClickListener(new SampleClickListener());
}
public void onActivityResult(int reqcode, int result, Intent it)
{
if(reqcode == SAMPLE_APP && result == RESULT_OK)
{
ArrayList<String> list = it.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
tv.setText(list.get(0));
}
}
class SampleClickListener implements OnClickListener
{
public void onClick(View v)
{
try
{
Intent it = new Intent();
it.setAction(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
it.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
it.putExtra(RecognizerIntent.EXTRA_PROMPT,"請輸入。");
startActivityForResult(it,SAMPLE_APP);
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "無法利用聲音辨識功能。", Toast.LENGTH_LONG).show();
}
}
}
}

 


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *