Skip to content Skip to sidebar Skip to footer

Retrieve The Call Number In My Android App

I would like to get the number call when i receive a call in my app.I would like to retrieve it and store it somewhere. public void onCallStateChanged(int state, String incomingNu

Solution 1:

Do you have the permissions?

Note that access to some telephony information is permission-protected. Your application won't receive updates for protected information unless it has the appropriate permissions declared in its manifest file. Where permissions apply, they are noted in the appropriate LISTEN_ flags.

http://developer.android.com/reference/android/telephony/PhoneStateListener.html

Solution 2:

Include following permissions in your application manifest as a child of application.

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

Also include a receiver in manifest

<!-- SAMPLE APPLICATION IS NAME OF OUR APPLICATION --><receiverandroid:name="SampleApplication"android:enabled="true"android:exported="true"android:permission=""><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /><actionandroid:name="android.intent.action.PHONE_STATE" /></intent-filter></receiver>

Create a class that extends android.telephony.PhoneStateListener and implement its onCallStateChanged

publicclassclassMyPhoneStateListenerextendsPhoneStateListener
  {
      publicvoidonCallStateChanged(int state, String incomingNumber)
      {
          Log.d("SampleApp","Incoming call from: "+incomingNumber);
      }
  }

Now in you onCreate() call TelephonyManager and add this class as listener:

TelephonyManagermyTelManager= (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
MyPhoneStateListenermyListner=newMyPhoneStateListener();
myTelManager.listen(myListner, PhoneStateListener.LISTEN_CALL_STATE);

You can check result in LogCat.

EDIT :Complete class which stores response in variable:

import android.app.Activity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

publicclassSampleAppextendsActivity{

private String incomingCallNumber="";
@OverridepublicvoidonCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.rating);
    TelephonyManagermyTelManager= 
(TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
    MyPhoneStateListenermyListner=newMyPhoneStateListener();
    myTelManager.listen(myListner, PhoneStateListener.LISTEN_CALL_STATE);


}
classMyPhoneStateListenerextendsPhoneStateListener
{
  publicvoidonCallStateChanged(int state, String incomingNumber)
  {
     SampleApp.this.incomingCallNumber=incomingNumber;
  }
}

}

Post a Comment for "Retrieve The Call Number In My Android App"