Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've very newly gotten into Android development, and decided that my first conquest on this fresh field would be to grasp how the phone reacted to incoming calls.

A little googling later led me to http://www.compiletimeerror.com/2013/08/android-call-state-listener-example.html#.Vi3Ren4vfwM (so my code shares a striking resemblance to his/hers).

My main (and only) activity looks like this:

Java
package com.example.x.telephonymanagertest;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        TelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    class TeleListener extends PhoneStateListener {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    // CALL_STATE_IDLE;
                    Log.i("WORDS", " I NEED TO HEAR  :" + incomingNumber);
                    Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE",
                            Toast.LENGTH_LONG).show();
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    // CALL_STATE_OFFHOOK;
                    Log.i("WORDS", " I NEED TO HEAR  :" + incomingNumber);

                    Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK",
                            Toast.LENGTH_LONG).show();
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    // CALL_STATE_RINGING
                    Log.i("WORDS", " I NEED TO HEAR  :" + incomingNumber);
                    Toast.makeText(getApplicationContext(), incomingNumber,
                            Toast.LENGTH_LONG).show();
                    Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING",
                            Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
            }
        }

    }
}


Now, here's where the fun stops. I got the app running on emulator, and used DDMS to spoof a few phone calls to my emulated device to see where the pieces landed.

And surely enough toast popped up and MyLittleDebugger flared up upon state swaps. The listener was working, however no number was ever being shown in my log or my toast.

It was just blank where the number should have been! Not null or anything, no, but blank!

After a little more googling, I realized that my AndroidManifest.xml might be the problem. It is as follows:

XML
<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.x.xy" >

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Now, here's the question: what am I missing?

Clearly, a little fraction of a something has gone wrong somewhere, because I am able to have my TelephonyMgr object .listen() to call states, but I can't get the number to show.
Posted
Comments
Afzaal Ahmad Zeeshan 26-Oct-15 16:58pm    
That is the case when you do not have the permission. Perhaps, I would recommend that you remove and re-install the application, this time having the permission predefined; while installing. For more, http://developer.android.com/reference/android/telephony/PhoneStateListener.html#onCallStateChanged(int, java.lang.String).
Member 12088907 27-Oct-15 2:31am    
What do you mean by "do not have permission" in this case? My AndroidManifest.xml clearly states that I want "android.permission.READ_PHONE_STATE". Is this not enough? Do I need to enable it somewhere else? Does it go in some other part of my XML?

See I've tried putting it a few places in the XML (between 'application' tags and between 'activity' tags) to no effect.
Richard MacCutchan 27-Oct-15 5:07am    
Everything looks OK with both code and manifest. How exactly are you passing the number in to the emulator?
Member 12088907 27-Oct-15 5:31am    
Yeah, that's what I thought too, which led me to such horrid confusion. Anyway, like mentioned I clicked the Android Device Monitor button from my Android Studio, then used DDMS, and clicked the "Emulator Control" tab. From there I used "Telephony Actions" and typed a random number and hit "Call".

It should also be mentioned that the number shows up perfectly in the normal "someone-is-calling-you-box/window" that comes with the default Android interface, so it's clearly recognizing the random number I typed is as an inbound phone number.

Based on my research, that should emulate a call perfectly.

Do you suspect some error in the emulation process?
Richard MacCutchan 27-Oct-15 5:35am    
Sorry it's impossible to guess. You could take a look at some of the articles at http://www.codeproject.com/KB/android/#Android+Tutorial+Contest to see if someone has posted a working sample.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900