Skip to main content

Android Simple Caller Talker Example

Android provides the facility to know the incoming number and speak it by the help of android speech apiand telephony manager.
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 option 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(this, this);  
  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>  


Output:

android caller talker example output 1

Comments

Popular posts from this blog

Android Tutorial

Android  is a complete set of software for mobile devices such as tablet computers, notebooks, smartphones, electronic book readers, set-top boxes etc. It contains a  linux-based Operating System ,  middleware  and  key mobile applications . It can be thought of as a mobile operating system. But it is not limited to mobile only. It is currently used in various devices such as mobiles, tablets, televisions etc. This tutorial is developed for beginners and experienced persons. Let's see the topics of android that we are going to learn. Basics of Android In this fundamental chapter, you will learn about android, its components, how to create first android application, internal of first android application etc. What is Android History and Version Software Stack Core Building Blocks Android Emulator Installing softwares Setup Eclipse Hello Android example Internal Details Dalvik VM AndroidManifest.xml R.java Hide Title Bar Activity and I...

A SHOULDER SURFING RESISTANT GRAPHICAL AUTHENTICATION SYSTEM

A SHOULDER SURFING RESISTANT GRAPHICAL AUTHENTICATION SYSTEM ABSTRACT Authentication based on passwords is used largely in applications for computer security and privacy. However, human actions such as choosing bad passwords and inputting passwords in an insecure way are regarded as”the weakest link” in the authentication chain. Rather than arbitrary alphanumeric strings, users tend to choose passwords either short or meaningful for easy memorization. With web applications and mobile apps piling up, people can access these applications anytime and anywhere with various devices. This evolution brings great convenience but also increases the probability of exposing passwords to shoulder surfing attacks. Attackers can observe directly or use external recording devices to collect users’ credentials. To overcome this problem, we proposed a novel authentication system PassMatrix, based on graphical passwords to resist shoulder surfing attacks. With a one-time valid login indicator and ...

ABSTRACT

MULTI USER GSM BASED DIGITAL SCROLLING LED DISPLAY WITH PASSWORD AND VOICE ABSTRACT The project mainly focuses on transmission of textual data through air interface by the use of GSM through asynchronous serial communication. The data will be processed by the microcontroller it programmed by embedded c language by using KEIL C compiler. The data will be displayed on led display on LED only after entering unique pass key. Existing System: How a reliable and an authentic wireless communication could be easily developed between a mobile phone and microcontroller using GSM (Global System for Mobile Communication) MODEM (Modulator-Demodulator). This technical paper explains GSM based e-notice board which can be widely used for multitude of applications including educational sector, traffic control, banks, and public advertisements, stoke exchanges etc. Proposed System: In this project we not only send the data, but send the data with pass code also. Which enables us to preve...