AdSense

AdSense3

Tuesday 20 October 2015

Android Simple Caller Talker Example

Android provides the facility to know the incoming number and speak it by the help of android speech api and TelephonyManager.

Here, we are going to develop a basic android app that speaks the incoming number while phone is in ringing mode.
In the next page, we will see the full version of this app that speaks the caller name and provides the Setting Options to change the speed rate and pitch. Additionaly, it provides option to add text before and after the incoming number or caller name.

activity_main.xml

We have not done anything special here. It has the simple textview.
File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="@string/hello_world" />  
  15.   
  16. </RelativeLayout>  

Activity class

In this activity, we have written the code to know the phone state, and speak the incoming number by the help of TextToSpeech class.
File: MainActivity.java
  1. package com.example.callertalker;  
  2. import java.util.Locale;  
  3. import android.media.AudioManager;  
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.telephony.PhoneStateListener;  
  8. import android.telephony.TelephonyManager;  
  9. import android.util.Log;  
  10. import android.widget.Toast;  
  11. import android.speech.tts.TextToSpeech;  
  12.   
  13. public class MainActivity extends Activity implements TextToSpeech.OnInitListener {  
  14.     private TextToSpeech tts;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.           
  20.         tts = new TextToSpeech(thisthis);  
  21.             
  22.         TelephonyManager telephonyManager = (TelephonyManager)getSystemService(  
  23.                                                                          Context.TELEPHONY_SERVICE);  
  24.           
  25.         PhoneStateListener callStateListener = new PhoneStateListener() {  
  26.         public void onCallStateChanged(int state, String incomingNumber){  
  27.               if(state==TelephonyManager.CALL_STATE_RINGING){  
  28.                   tts.speak(incomingNumber+" calling", TextToSpeech.QUEUE_FLUSH, null);  
  29.                   Toast.makeText(getApplicationContext(),"Phone is Ringing : "+incomingNumber,   
  30.                                                                                Toast.LENGTH_LONG).show();  
  31.                  }  
  32.               if(state==TelephonyManager.CALL_STATE_OFFHOOK){  
  33.                     Toast.makeText(getApplicationContext(),"Phone in a call or call picked",   
  34.                                                                                   Toast.LENGTH_LONG).show();  
  35.               }  
  36.               if(state==TelephonyManager.CALL_STATE_IDLE){  
  37.                     //phone is neither ringing nor in a call  
  38.               }  
  39.         }  
  40.         };  
  41.         telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);  
  42.     }  
  43.   
  44.     @Override  
  45.     public void onInit(int status) {  
  46.         if (status == TextToSpeech.SUCCESS) {  
  47.             int result = tts.setLanguage(Locale.US);  
  48.             if (result == TextToSpeech.LANG_MISSING_DATA  
  49.                     || result == TextToSpeech.LANG_NOT_SUPPORTED) {  
  50.                 Log.e("TTS""This Language is not supported");  
  51.             } else {          
  52.             }  
  53.   
  54.         } else {  
  55.             Log.e("TTS""Initilization Failed!");  
  56.         }  
  57.     }  
  58.       
  59.     @Override  
  60.     public void onDestroy() {  
  61.     // Don't forget to shutdown tts!  
  62.     if (tts != null) {  
  63.         tts.stop();  
  64.         tts.shutdown();  
  65.     }  
  66.     super.onDestroy();  
  67.     }  
  68. }  

AndroidManifest.xml

You need to add READ_PHONE_STATE uses permission in this xml file. Let's see the full code.
File: AndroidManifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:androclass="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.callertalker"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="17" />  
  10.   
  11.    <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
  12.        
  13.     <application  
  14.         android:allowBackup="true"  
  15.         android:icon="@drawable/ic_launcher"  
  16.         android:label="@string/app_name"  
  17.         android:theme="@style/AppTheme" >  
  18.         <activity  
  19.             android:name="com.example.callertalker.MainActivity"  
  20.             android:label="@string/app_name" >  
  21.             <intent-filter>  
  22.                 <action android:name="android.intent.action.MAIN" />  
  23.   
  24.                 <category android:name="android.intent.category.LAUNCHER" />  
  25.             </intent-filter>  
  26.         </activity>  
  27.     </application>  
  28.   
  29. </manifest>  

No comments:

Post a Comment