?
?頁面如下:
?
?
?
?
?
?
主頁面的布局分隔為三部分:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gztab_content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation=" vertical " > <!-- header --> <include android:id="@+id/gz_top" layout="@layout/gzmgr_top_panel" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <!-- content --> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TabWidget android:id ="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" > </TabWidget> <FrameLayout android:id ="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id ="@+id/unhanlderLayout "> <Button android:id="@+id/gzbtn_unhandler_refresh" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="刷新" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/gz_unhandler_ListView" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id ="@+id/handlingLayout"> <Button android:id="@+id/gzbtn_handlering_refresh" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="刷新" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/gz_handleringListView" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id ="@+id/handledLayout"> <Button android:id="@+id/gzbtn_handled_refresh" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="刷新" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/gzgzbtn_handled_ListView" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> <!-- bottom --> <Button android:id="@+id/btnManyInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="更多(10)"/> </LinearLayout>
?
注意觀察上面標記為紅色的android:id均采用android系統(tǒng)默認的名稱:
?
頁面的導(dǎo)航組件:
<?xml version="1.0" encoding="UTF-8"?> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:id="@+id/top_gzbtn_left" android:text="返回" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="12.0dip" android:layout_alignParentLeft="true" android:layout_centerVertical="true" /> <Button android:id="@+id/top_gzbtn_right" android:text="發(fā)布" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="12.0dip" android:layout_alignParentRight="true " android:layout_centerVertical="true " /> <TextView android:textSize="22.0sp" android:textColor="#ffffffff" android:ellipsize="middle" android:gravity="center_horizontal" android:id="@+id/top_gztxt_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="故障管理 " android:singleLine="true" android:layout_toLeftOf="@+id/top_gzbtn_right" android:layout_toRightOf="@+id/top_btn_left0" android:layout_centerInParent="true" android:layout_alignWithParentIfMissing="true" > </TextView> </RelativeLayout>
?
上面紅色布局主要采用相對布局定位相關(guān)的導(dǎo)航位置:
?
?
每一個tab頁面的中的內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- 個人信息 --> <LinearLayout android:id="@+id/gzPersonInfoLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <ImageView android:id="@+id/gzimg_item_Img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px" android:layout_gravity="left" android:src="@drawable/ic_launcher"/> <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:shrinkColumns="1" android:layout_gravity="right" > <TableRow > <TextView android:id="@+id/gztxt_item_userCode" android:padding="3dip" /> <TextView android:id="@+id/gztxt_item_UserCollectionName" android:padding="3dip" /> <TextView android:id="@+id/gztxt_item_Time" android:padding="3dip" /> </TableRow> <TableRow > <TextView android:id="@+id/gztxt_item_name_person" android:padding="3dip" /> <TextView android:text="" android:padding="3dip" /> <TextView android:padding="3dip" /> </TableRow> </TableLayout> </LinearLayout> <LinearLayout android:id="@+id/gzItemLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableRow> <TextView android:text="項目名稱:" android:padding="3dip" /> <TextView android:id="@+id/gztxt_item_name" android:padding="3dip" /> <TextView android:text="列車編號:" android:padding="3dip" /> <TextView android:id="@+id/gztxt_item_code" android:padding="3dip" /> </TableRow> </TableLayout> </LinearLayout> <LinearLayout android:id="@+id/gzItemLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:shrinkColumns="1" android:layout_gravity="right" > <TableRow > <TextView android:text="故障負責(zé)人" android:padding="3dip" /> <TextView android:id="@+id/gztxt_item_user" android:padding="3dip" /> </TableRow> <TableRow > <TextView android:text="故障處理人" android:padding="3dip" /> <TextView android:id="@+id/gztxt_item_handler" android:padding="3dip" /> </TableRow> <TableRow > <TextView android:text="故障描述" android:padding="3dip" /> <TextView android:id="@+id/gztxt_item_descption" android:padding="3dip" /> </TableRow> <TableRow > <TextView android:text="問題級別:" android:padding="3dip" /> <TextView android:id="@+id/gztxt_item_level" android:text="一般" android:padding="3dip" /> </TableRow> <TableRow > <TextView android:text="狀態(tài):" android:padding="3dip" /> <TextView android:id="@+id/gztxt_item_status" android:text="未解決" android:padding="3dip" /> </TableRow> </TableLayout> </LinearLayout> </LinearLayout>
?
?
?
?
訊飛的語音sdk是需要申請的,地址是: http://dev.voicecloud.cn/developer.php?vt=1 ?。申請一個訊飛的開發(fā)者賬號,然后申請一個appid,申請的時候需要填寫開發(fā)者信息和你的應(yīng)用的信息。
申請之后經(jīng)過審核通過,會得到一個appid,這個是在使用語音服務(wù)時需要用到的。(筆者感覺申請還是比較容易通過的,簡單地填寫一下就通過了。速度也很快,我在晚上十一點多申請的,到第二天早上九點多就收到審核通過的郵件。個人感覺這個審核只是為了防止惡意使用,畢竟語音服務(wù)是要使用訊飛的服務(wù)器資源的。)
申請到appid之后就可以下載SDK了,然后使用語音服務(wù)了。
以下我試用的過程,(點擊button,彈出語音框,說完之后將識別的結(jié)果顯示在EditText中 ):
?
- 在eclipse里新建一個android工程
- 導(dǎo)入訊飛的語音jar包
-
訊飛的服務(wù)是需要一堆權(quán)限的,在manifest.xml中加入
- < uses-permission ? android:name = "android.permission.RECORD_AUDIO" ? /> ??
- < uses-permission ? android:name = "android.permission.INTERNET" ? /> ??
- < uses-permission ? android:name = "android.permission.ACCESS_NETWORK_STATE" ? /> ??
- < uses-permission ? android:name = "android.permission.ACCESS_WIFI_STATE" ? /> ??
- < uses-permission ? android:name = "android.permission.CHANGE_NETWORK_STATE" ? /> ??
- < uses-permission ? android:name = "android.permission.READ_PHONE_STATE" ? /> ??
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
分別為:通過麥克風(fēng)錄音、聯(lián)網(wǎng)、獲取網(wǎng)絡(luò)信息狀態(tài)、獲取wifi狀態(tài)、改變網(wǎng)絡(luò)狀態(tài)如是否能聯(lián)網(wǎng)、訪問電話狀態(tài) -
在main.xml中添加一個EditText和一個Button,如下
- <? xml ? version = "1.0" ? encoding = "utf-8" ?> ??
- < LinearLayout ? xmlns:android = "http://schemas.android.com/apk/res/android" ??
- ???? android:layout_width = "fill_parent" ??
- ???? android:layout_height = "fill_parent" ??
- ???? android:orientation = "vertical" ? > ??
- ??
- ???? < EditText ??
- ???????? android:id = "@+id/editText" ??
- ???????? android:layout_width = "fill_parent" ??
- ???????? android:layout_height = "300dp" ??
- ???????? android:gravity = "top" ??
- ???????? android:inputType = "textMultiLine" ? > ??
- ??
- ???????? < requestFocus ? /> ??
- ???? </ EditText > ??
- ??
- ???? < Button ??
- ???????? android:id = "@+id/button_start" ??
- ???????? android:layout_width = "wrap_content" ??
- ???????? android:layout_height = "wrap_content" ??
- ???????? android:text = "點擊開始說話" ? /> ??
- ??
- </ LinearLayout > ??
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/editText" android:layout_width="fill_parent" android:layout_height="300dp" android:gravity="top" android:inputType="textMultiLine" > <requestFocus /> </EditText> <Button android:id="@+id/button_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="點擊開始說話" /> </LinearLayout>
- 然后在MainActivity中編寫代碼實現(xiàn)了
?
(這里可以看到在線的文檔: http://dev.voicecloud.cn/developer.php?category=YW5kcm9pZA%3D%3D&column=ZG9jdW1lbnQ%3D&type=d2lkZ2V0 )
通過閱讀訊飛給的文檔,可以發(fā)現(xiàn)標準的識別控件是RecognizerDialog——是一個Dialog的子類,所以我們是可以在Activity中通過showDialog(int)方法來調(diào)用它的。
重寫Activity的方法
- @Override ??
- protected ?Dialog?onCreateDialog( int ?id)?{}??
@Override protected Dialog onCreateDialog(int id) {}
在其中創(chuàng)建并設(shè)置好一個RecognizerDialog即可。
?
創(chuàng)建 RecognizerDialog 方法為
- RecognizerDialog?recognizerDialog?=? new ?RecognizerDialog(??
- ????????MainActivity. this ,? "appid=1234567" ); //?這里應(yīng)該寫從科大訊飛申請到的appid ??
RecognizerDialog recognizerDialog = new RecognizerDialog( MainActivity.this, "appid=1234567");// 這里應(yīng)該寫從科大訊飛申請到的appid
(其中appid應(yīng)該寫自己從訊飛申請到的appid,由于協(xié)議的問題,我不便把自己申請的id公開,所以這里用了1234567來代替。根據(jù)訊飛的說明,非法的appid是不能使用語音服務(wù)的,但是我用隨機數(shù)字試驗了一下,居然也是能用的,不知道是不是BUG。)
?
? ? ? ? ? ? ? ? ? ? ? ? ? 官方的文檔:
? ? ? ?
然后需要設(shè)置識別參數(shù)
- recognizerDialog.setEngine( "sms" ,? null ,? null );??
recognizerDialog.setEngine("sms", null, null);
第一個參數(shù)“sms”表示為請求的服務(wù)為“語音識別”。后兩個參數(shù)暫時設(shè)為null就好。
?
? ? ? ? ? ? ?官方文檔:
最后還需給recognizerDialog設(shè)置一個listener,回調(diào)接口用以獲取結(jié)果,
recognizerDialog.setListener(RecognizerDialogListener listener)的參數(shù)為RecognizerDialogListener接口,實現(xiàn)此接口要重寫兩個方法:onResults(ArrayList results,boolean isLast)和onEnd(SpeechError error)。用以獲取和處理結(jié)果。
我的實現(xiàn)為直接寫了一個匿名類:
- recognizerDialog.setListener( new ?RecognizerDialogListener()?{??
- ???? @Override ??
- ???? public ? void ?onResults(ArrayList<RecognizerResult>?results,??
- ???????????? boolean ?arg1)?{??
- ????????StringBuffer?result?=? new ?StringBuffer();??
- ???????? for ?(RecognizerResult?r?:?results)?{??
- ????????????result.append(r.text);??
- ????????}??
- ????????editText.setText(result.toString());??
- ????}??
- ??
- ???? @Override ??
- ???? public ? void ?onEnd(SpeechError?arg0)?{??
- ??
- ????}??
- });??
recognizerDialog.setListener(new RecognizerDialogListener() { @Override public void onResults(ArrayList<RecognizerResult> results, boolean arg1) { StringBuffer result = new StringBuffer(); for (RecognizerResult r : results) { result.append(r.text); } editText.setText(result.toString()); } @Override public void onEnd(SpeechError arg0) { } });
然后將此RecognizerDialog返回即可。
?
完整的MainActivity代碼:
?
- import ?com.iflytek.speech.RecognizerResult;??
- import ?com.iflytek.speech.SpeechError;??
- import ?com.iflytek.ui.RecognizerDialog;??
- import ?com.iflytek.ui.RecognizerDialogListener;??
- ??
- public ? class ?MainActivity? extends ?Activity?{??
- ??
- ????EditText?editText?=? null ;??
- ????Button?startButton?=? null ;??
- ??
- ???? @Override ??
- ???? public ? void ?onCreate(Bundle?savedInstanceState)?{??
- ???????? super .onCreate(savedInstanceState);??
- ????????setContentView(R.layout.main);??
- ????????editText?=?(EditText)?findViewById(R.id.editText);??
- ????????startButton?=?(Button)?findViewById(R.id.button_start);??
- ????????startButton.setOnClickListener( new ?OnClickListener()?{??
- ???????????? @Override ??
- ???????????? public ? void ?onClick(View?v)?{??
- ????????????????showDialog( 1 );??
- ????????????}??
- ????????});??
- ????}??
- ??
- ???? @Override ??
- ???? protected ?Dialog?onCreateDialog( int ?id)?{??
- ????????RecognizerDialog?recognizerDialog?=? new ?RecognizerDialog(??
- ????????????????MainActivity. this ,? "appid=1234567" ); //?這里應(yīng)該寫從科大訊飛申請到的appid ??
- ????????recognizerDialog.setEngine( "sms" ,? null ,? null );??
- ????????recognizerDialog.setListener( new ?RecognizerDialogListener()?{??
- ???????????? @Override ??
- ???????????? public ? void ?onResults(ArrayList<RecognizerResult>?results,??
- ???????????????????? boolean ?arg1)?{??
- ????????????????StringBuffer?result?=? new ?StringBuffer();??
- ???????????????? for ?(RecognizerResult?r?:?results)?{??
- ????????????????????result.append(r.text);??
- ????????????????}??
- ????????????????editText.setText(result.toString());??
- ????????????}??
- ??
- ???????????? @Override ??
- ???????????? public ? void ?onEnd(SpeechError?arg0)?{??
- ??
- ????????????}??
- ????????});??
- ???????? return ?recognizerDialog;??
- ????}??
- }??
import com.iflytek.speech.RecognizerResult; import com.iflytek.speech.SpeechError; import com.iflytek.ui.RecognizerDialog; import com.iflytek.ui.RecognizerDialogListener; public class MainActivity extends Activity { EditText editText = null; Button startButton = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText = (EditText) findViewById(R.id.editText); startButton = (Button) findViewById(R.id.button_start); startButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showDialog(1); } }); } @Override protected Dialog onCreateDialog(int id) { RecognizerDialog recognizerDialog = new RecognizerDialog( MainActivity.this, "appid=1234567");// 這里應(yīng)該寫從科大訊飛申請到的appid recognizerDialog.setEngine("sms", null, null); recognizerDialog.setListener(new RecognizerDialogListener() { @Override public void onResults(ArrayList<RecognizerResult> results, boolean arg1) { StringBuffer result = new StringBuffer(); for (RecognizerResult r : results) { result.append(r.text); } editText.setText(result.toString()); } @Override public void onEnd(SpeechError arg0) { } }); return recognizerDialog; } }
程序在真機運行截圖,經(jīng)過檢驗,科大訊飛的識別率還是很高的。?
??
?
demo下載地址: http://download.csdn.net/detail/barryhappy/4178459
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
