• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps i want to get caller name while incoming call, i am using this below code ,but it's not working..

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();
}
}
 
Hello Anil kumar k. I'm moving this thread into the development area where it will get the attention you seek. Welcome to Android Forums. Good luck.
 
Not really dabbled in this side as yet but from what I have found using google, there are permissions required in the android manifest such as read the contacts and read phone state

This link might be of help it does not cover the TTS side but it could point you in the right direction -> Detecting incoming and outgoing phone calls on Android - CodeProject

Found this post here too which might help -> http://androidforums.com/application-development/24224-retrieve-incoming-call-s-phone-number-in-android.html

And another here as it mentions another permission too -> http://learnandroideasily.blogspot.co.uk/2012/11/how-to-handle-incoming-calls-in-android.html

Thanks

TimCS
 
Back
Top Bottom