Retrieve incoming call's phone number in Android

耗尽温柔 提交于 2019-11-26 01:06:49

问题


I would like to retrieve the incoming call\'s phonenumber and do something with it like the do in http://blog.whitepages.com/2009/02/27/caller-id-by-whitepages-a-new-android-app-that-puts-telemarketers-on-alert/

Could you please help me because I can\'t find any information about this. Where do i start and how do i get hold of the phonenumber?


Ok so currently my code looks like below. When I place the call the CustomBroadcastReceiver catches it and the log message is printed out. I can retrieve the telephone number from the bundle. But! I can\'t get hte CustomPhoneStateListener to work. As you can see I have registered my customPhoneState listener to the receiver but the log message never get\'s printed out from the CustomPhoneStateListener class. What am I my missing here? Is my thinking correct?


<receiver android:name=\".CustomBroadcastReceiver\">
        <intent-filter>
            <action android:name=\"android.intent.action.PHONE_STATE\" /> 
        </intent-filter>
</receiver>

</application>
<uses-sdk android:minSdkVersion=\"5\" />
<uses-permission android:name=\"android.permission.INTERNET\" />
<uses-permission android:name=\"android.permission.WRITE_CONTACTS\" />
<uses-permission android:name=\"android.permission.READ_PHONE_STATE\" />

public class CustomPhoneStateListener extends PhoneStateListener {

private static final String TAG = \"CustomPhoneStateListener\";

public void onCallStateChange(int state, String incomingNumber){

    Log.v(TAG, \"WE ARE INSIDE!!!!!!!!!!!\");
    Log.v(TAG, incomingNumber);

    switch(state){
        case TelephonyManager.CALL_STATE_RINGING:
            Log.d(TAG, \"RINGING\");
            break;
    }   
}

public class CustomBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = \"CustomBroadcastReceiver\";

@Override
public void onReceive(Context context, Intent intent) {
    Log.v(TAG, \"WE ARE INSIDE!!!!!!!!!!!\");
    TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();

    telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);


    Bundle bundle = intent.getExtras();
    String phoneNr= bundle.getString(\"incoming_number\");
    Log.v(TAG, \"phoneNr: \"+phoneNr);

}

回答1:


Use PhoneStateListener. It has an onCallStateChanged handler; one of the supplied arguments you'll get is a String containing the incoming phone number.




回答2:


Your overridden method in CustomPhoneStateListener should be called onCallStateChanged() (and not onCallStateChange()).

This would have been spotted by the Java compiler if you would have had the @Override annotation, like you have for onReceive().



来源:https://stackoverflow.com/questions/1853220/retrieve-incoming-calls-phone-number-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!