Read.java
package sms.TextToSpeech;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.SyncStateContract.Constants;
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.TextView;
public class Read extends Activity implements OnInitListener{
/** Called when the activity is first created. */
private EditText et;
private Button b;
private String address;
private String body;
private String date;
private TextToSpeech mTts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button)findViewById(R.id.b);
et=(EditText)findViewById(R.id.et);
et.setEnabled(false);
et.setClickable(false);
et.setBackgroundColor(Color.WHITE);
Uri SmsUri=Uri.parse("content://sms/inbox");
String[] projection=new String[]{"_id","address","body","date"};
Cursor cursor=null;
try{
cursor=getContentResolver().query(SmsUri,projection,null,null,null);//Bilgilerin nereden alinacagi belirlenir
if(cursor!=null&&cursor.moveToFirst()){ //Ilk mesaja konumlanir
int id=cursor.getInt(cursor.getColumnIndex("_id")); //Id sini alir
address=cursor.getString(cursor.getColumnIndex("address")); //hangi telefondan geldigini alir
body=cursor.getString(cursor.getColumnIndex("body")); //mesaj
date=cursor.getString(cursor.getColumnIndex("date")); //mesaj atilan tarih
SimpleDateFormat formatter=new SimpleDateFormat("dd/MM/yyyy - HH:mm:ss");
date=formatter.format(new Date(Long.parseLong(date)));
et.setText(body);
}
}
finally{
if(cursor!=null){
cursor.close();
}
}
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onInit(1); //Click eventi olustugunda text to speech cagirilir
}
});
mTts=new TextToSpeech(this,this);
}
//TTS=Text to Speech
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if(b.isPressed()){
Locale loc = new Locale("en", "",""); //TTS ayarlari
if(mTts.isLanguageAvailable(loc) >= TextToSpeech.LANG_AVAILABLE){
mTts.setLanguage(loc); //Dil ayarlanir
}
mTts.speak(et.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);//Konusma islemi gerceklestirilir
}
}
protected void onDestroy() {//TTS destroy eder
super.onDestroy();
mTts.shutdown();
}
}AndroidManifest.xml dosyasi iceriside <uses-permission android:name="android.permission.READ_SMS" />
Main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/et" android:layout_width="fill_parent" android:layout_height="150px" android:gravity="top" /> <Button android:id="@+id/b" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Speak" /> </LinearLayout>
0 yorum:
Yorum Gönder