Anil kumar k
Lurker
public class MainActivity extends Activity implements TextToSpeech.OnInitListener {
private TextToSpeech tts;
String name;
String number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(
Context.TELEPHONY_SERVICE);
PhoneStateListener callStateListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber){
if(state==TelephonyManager.CALL_STATE_RINGING){
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor people = getContentResolver().query(uri, projection, null, null, null);
int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
people.moveToFirst();
do {
name = people.getString(indexName);
number = people.getString(indexNumber);
// Do work...
} while (people.moveToNext());
if(name!=null&&number==incomingNumber){
// tts.speak(incomingNumber+" calling", TextToSpeech.QUEUE_FLUSH, null);
tts.speak(name+" calling", TextToSpeech.QUEUE_FLUSH, null);
// tts.speak(number+" calling", TextToSpeech.QUEUE_FLUSH, null);
//Toast.makeText(getApplicationContext(),"Phone is Ringing : "+incomingNumber, Toast.LENGTH_LONG).show();
}
else
{
tts.speak(incomingNumber+" calling", TextToSpeech.QUEUE_FLUSH, null);
//Toast.makeText(MainActivity.this, "no data", Toast.LENGTH_LONG).show();
}
}
if(state==TelephonyManager.CALL_STATE_OFFHOOK){
Toast.makeText(getApplicationContext(),"Phone in a call or call picked",
Toast.LENGTH_LONG).show();
}
if(state==TelephonyManager.CALL_STATE_IDLE){
//phone is neither ringing nor in a call
}
}
};
telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);
}
@Override
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) {
Log.e("TTS", "This Language is not supported");
} else {
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}
private TextToSpeech tts;
String name;
String number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(
Context.TELEPHONY_SERVICE);
PhoneStateListener callStateListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber){
if(state==TelephonyManager.CALL_STATE_RINGING){
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor people = getContentResolver().query(uri, projection, null, null, null);
int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
people.moveToFirst();
do {
name = people.getString(indexName);
number = people.getString(indexNumber);
// Do work...
} while (people.moveToNext());
if(name!=null&&number==incomingNumber){
// tts.speak(incomingNumber+" calling", TextToSpeech.QUEUE_FLUSH, null);
tts.speak(name+" calling", TextToSpeech.QUEUE_FLUSH, null);
// tts.speak(number+" calling", TextToSpeech.QUEUE_FLUSH, null);
//Toast.makeText(getApplicationContext(),"Phone is Ringing : "+incomingNumber, Toast.LENGTH_LONG).show();
}
else
{
tts.speak(incomingNumber+" calling", TextToSpeech.QUEUE_FLUSH, null);
//Toast.makeText(MainActivity.this, "no data", Toast.LENGTH_LONG).show();
}
}
if(state==TelephonyManager.CALL_STATE_OFFHOOK){
Toast.makeText(getApplicationContext(),"Phone in a call or call picked",
Toast.LENGTH_LONG).show();
}
if(state==TelephonyManager.CALL_STATE_IDLE){
//phone is neither ringing nor in a call
}
}
};
telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);
}
@Override
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) {
Log.e("TTS", "This Language is not supported");
} else {
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}