簡訊語音應用

1. AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app3"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.RECEIVE_SMS" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.app3.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
     
    </application>


</manifest>

2. activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/input_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="Receive SMS">

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/speak_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Speak" />


</LinearLayout>

3. MainActivity.java

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.telephony.SmsMessage;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnInitListener {
/** Called when the activity is first created. */
private final static String MSG_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private Button speak_btn;
private EditText input_et;
private TextToSpeech myTTS;
private StringBuilder sb;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input_et = (EditText) findViewById(R.id.input_et);
speak_btn = (Button) findViewById(R.id.speak_btn);
myTTS = new TextToSpeech(this, this);
registerReceiver(mBroadcastReceiver, new IntentFilter(MSG_RECEIVED));
speak_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {

SpeakLanguage(myTTS, input_et.getText().toString());

}
});
}

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(MSG_RECEIVED)) {
sb = new StringBuilder();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] obj = (Object[]) bundle.get("pdus");
// 建立簡訊物件
SmsMessage[] message = new SmsMessage[obj.length];
for (int i = 0; i < obj.length; i++) {
message[i] = SmsMessage.createFromPdu((byte[]) obj[i]);
}
for (SmsMessage currentMessage : message) {
sb.append("phone from ");
// 發送人的電話號碼
sb.append(InsertSpaceIntoPhNumber(currentMessage
.getDisplayOriginatingAddress())); // 發送的資訊內容
sb.append("Sms : "
+ currentMessage.getDisplayMessageBody());
}
}
}

input_et.setText(sb.toString());
Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
}
};

public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
myTTS.setEngineByPackageName(myTTS.getDefaultEngine());
Toast.makeText(
MainActivity.this,
"TTS Engine : " + myTTS.getDefaultEngine().toString()
+ "\n" + "Language : "
+ myTTS.getLanguage().toString(),
Toast.LENGTH_SHORT).show();

} else {
Toast.makeText(MainActivity.this, "Error Engine",
Toast.LENGTH_SHORT).show();
}

}

protected void onDestroy() {
if (myTTS != null) {
myTTS.stop();
myTTS.shutdown();
}
unregisterReceiver(mBroadcastReceiver);
super.onDestroy();
}

private void SpeakLanguage(TextToSpeech mSpeech, String str) {

mSpeech.speak(str, TextToSpeech.QUEUE_ADD, null);

}

public String InsertSpaceIntoPhNumber(String s) {
String temp = "";
for (int i = 0; i < s.length(); i++) {
temp += s.charAt(i) + " ";
}
return temp + "\n";
}


}

沒有留言: