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(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>  


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...

CLOUD WORKFLOW SCHEDULING WITH DEADLINE AND TIME SLOT ALGORITHM

CLOUD WORKFLOW SCHEDULING WITH DEADLINE AND TIME SLOT ALGORITHM Abstract Allocating service capacities in cloud computing is based on the assumption that they are unlimited and can be used at any time. However, available service capacities change with workload and cannot satisfy users’ requests at any time from the cloud provider’s perspective because cloud services can be shared by multiple tasks. Cloud service providers provide available time slots for new user’s requests based on available capacities. In this paper, we consider workflow scheduling with deadline and time slot availability in cloud computing. An iterated heuristic framework is presented for the problem under study which mainly consists of initial solution construction, improvement, and perturbation. Three initial solution construction strategies, two greedy- and fair-based improvement strategies and a perturbation strategy are proposed. Different strategies in the three phases result in several heuristics. ...

MobiContext: A Context-aware Cloud-Based Venue Recommendation Framework

            MobiContext: A Context-aware Cloud-Based Venue Recommendation Framework ABSTRACT  In recent years, recommendation systems have seen significant evolution in the field of knowledge engineering. Most of the existing recommendation systems based their models on collaborative filtering approaches that make them simple to implement. However, performance of most of the existing collaborative filtering-based recommendation system suffers due to the challenges, such as: (a) cold start, (b) data sparseness, and (c) scalability. Moreover, recommendation problem is often characterized by the presence of many conflicting objectives or decision variables, such as users’ preferences and venue closeness. In this paper, we proposed MobiContext , a hybrid cloud-based Bi-Objective Recommendation Framework (BORF) for mobile social networks. The MobiContext utilizes multi-objective optimization techniques to generate personalized recommendat...