2014-05-04 75 views
0

我有以下代碼。 第一個代碼從Edittext獲取文本並轉換爲語音。 第二個代碼是從收件箱中獲取短信文本。 我的項目正在將短信文本轉換爲語音。 如何使兩個代碼之間的關係和使用2個活動?將短信文本轉換爲android中的語音

請幫幫我。從針對的EditText獲取文本 代碼:

package com.prgguru.android; 
import java.util.Locale; 
import android.app.Activity; 
import android.os.Bundle; 
import android.speech.tts.TextToSpeech; 
import android.speech.tts.TextToSpeech.OnInitListener; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class TexttoSpeechActivity extends Activity implements OnInitListener { 
    private TextToSpeech tts; 
    private Button btnSpeak; 
    private EditText txtText; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     tts = new TextToSpeech(this, this); 
     btnSpeak = (Button) findViewById(R.id.btnSpeak); 
     txtText = (EditText) findViewById(R.id.txtText); 

     btnSpeak.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       speakOut(); 
      } 
     }); 
    } 

    @Override 
    public void onDestroy() { 
     // Don't forget to shutdown! 
     if (tts != null) { 
      tts.stop(); 
      tts.shutdown(); 
     } 
     super.onDestroy(); 
    } 

    public void onInit(int status) { 
     if (status == TextToSpeech.SUCCESS) { 
      int result = tts.setLanguage(Locale.US); 
      if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
       Toast.makeText(this, "Language not supported", Toast.LENGTH_LONG).show(); 
       Log.e("TTS", "Language is not supported"); 
      } else { btnSpeak.setEnabled(true); } 
     } else { Log.e("TTS", "Initilization Failed"); } 
    } 

    private void speakOut() { 
     String text = txtText.getText().toString(); 
     if (text.length() == 0) { 
      tts.speak("You haven't typed text", TextToSpeech.QUEUE_FLUSH, null); 
     } else { 
      tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); 
     } 
    } 
} 

用於獲取短信郵件從收件箱是代碼?

package com.android.www; 

import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.widget.Toast; 

public class smstext extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    TextView view = new TextView(this); 
    Uri uriSMSURI = Uri.parse("content://sms/inbox"); 
    Cursor cur =getContentResolver().query(uriSMSURI, null, null, null, null); 
    String sms=""; 
    while (cur.moveToNext()){ 
     sms +="From:"+ cur.getString(2)+":"+cur.getString(11)+"\n"; 
    } 
    view.setText(sms); 
    setContentView(view); 


    } 
    } 

回答

0

http://pulse7.net/android/read-sms-message-inbox-sent-draft-android/此外還有草稿和發送框的代碼。

// Create Inbox box URI 
Uri inboxURI = Uri.parse("content://sms/inbox"); 

// List required columns 
String[] reqCols = new String[] { "_id", "address", "body" }; 

// Get Content Resolver object, which will deal with Content Provider 
ContentResolver cr = getContentResolver(); 

// Fetch Inbox SMS Message from Built-in Content Provider 
Cursor c = cr.query(inboxURI, reqCols, null, null, null); 
相關問題