?
?頁面如下:
?
?
?
?
?
?
主頁面的布局分隔為三部分:
<?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系統默認的名稱:
?
頁面的導航組件:
<?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="發布"
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>
?
上面紅色布局主要采用相對布局定位相關的導航位置:
?
?
每一個tab頁面的中的內容如下:
<?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="故障負責人" 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="狀態:" 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 ?。申請一個訊飛的開發者賬號,然后申請一個appid,申請的時候需要填寫開發者信息和你的應用的信息。
申請之后經過審核通過,會得到一個appid,這個是在使用語音服務時需要用到的。(筆者感覺申請還是比較容易通過的,簡單地填寫一下就通過了。速度也很快,我在晚上十一點多申請的,到第二天早上九點多就收到審核通過的郵件。個人感覺這個審核只是為了防止惡意使用,畢竟語音服務是要使用訊飛的服務器資源的。)
申請到appid之后就可以下載SDK了,然后使用語音服務了。
以下我試用的過程,(點擊button,彈出語音框,說完之后將識別的結果顯示在EditText中 ):
?
- 在eclipse里新建一個android工程
- 導入訊飛的語音jar包
-
訊飛的服務是需要一堆權限的,在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" />分別為:通過麥克風錄音、聯網、獲取網絡信息狀態、獲取wifi狀態、改變網絡狀態如是否能聯網、訪問電話狀態 -
在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中編寫代碼實現了
?
(這里可以看到在線的文檔: http://dev.voicecloud.cn/developer.php?category=YW5kcm9pZA%3D%3D&column=ZG9jdW1lbnQ%3D&type=d2lkZ2V0 )
通過閱讀訊飛給的文檔,可以發現標準的識別控件是RecognizerDialog——是一個Dialog的子類,所以我們是可以在Activity中通過showDialog(int)方法來調用它的。
重寫Activity的方法
- @Override ??
- protected ?Dialog?onCreateDialog( int ?id)?{}??
@Override
protected Dialog onCreateDialog(int id) {}
在其中創建并設置好一個RecognizerDialog即可。
?
創建 RecognizerDialog 方法為
- RecognizerDialog?recognizerDialog?=? new ?RecognizerDialog(??
- ????????MainActivity. this ,? "appid=1234567" ); //?這里應該寫從科大訊飛申請到的appid ??
RecognizerDialog recognizerDialog = new RecognizerDialog(
MainActivity.this, "appid=1234567");// 這里應該寫從科大訊飛申請到的appid
(其中appid應該寫自己從訊飛申請到的appid,由于協議的問題,我不便把自己申請的id公開,所以這里用了1234567來代替。根據訊飛的說明,非法的appid是不能使用語音服務的,但是我用隨機數字試驗了一下,居然也是能用的,不知道是不是BUG。)
?
? ? ? ? ? ? ? ? ? ? ? ? ? 官方的文檔:
? ? ? ?
然后需要設置識別參數
- recognizerDialog.setEngine( "sms" ,? null ,? null );??
recognizerDialog.setEngine("sms", null, null);
第一個參數“sms”表示為請求的服務為“語音識別”。后兩個參數暫時設為null就好。
?
? ? ? ? ? ? ?官方文檔:
最后還需給recognizerDialog設置一個listener,回調接口用以獲取結果,
recognizerDialog.setListener(RecognizerDialogListener listener)的參數為RecognizerDialogListener接口,實現此接口要重寫兩個方法:onResults(ArrayList results,boolean isLast)和onEnd(SpeechError error)。用以獲取和處理結果。
我的實現為直接寫了一個匿名類:
- 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" ); //?這里應該寫從科大訊飛申請到的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");// 這里應該寫從科大訊飛申請到的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;
}
}
程序在真機運行截圖,經過檢驗,科大訊飛的識別率還是很高的。?
??
?
demo下載地址: http://download.csdn.net/detail/barryhappy/4178459
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

