在Android中检索来电的电话号码

我想检索来电的电话号码,并像http://blog.whitepages.com/2009/02/27/caller-id-by-whitepages-a-new-android-app-即,看跌期权-电话销售上的警报/

你能帮我吗,因为我找不到任何有关这方面的信息。 我从哪里开始,如何获取数字?


好,所以目前我的代码如下所示。 当我拨打电话时,CustomBroadcastReceiver捕获它并打印出日志信息。 我可以从捆绑中找回电话号码。 但! 我无法使CustomPhoneStateListener工作。 正如你所看到的,我已经将自定义电话状态监听器注册到接收器,但日志消息永远不会从CustomPhoneStateListener类打印出来。 我在这里错过了什么? 我的想法是否正确?


<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); } 

使用PhoneStateListener 。 它有一个onCallStateChanged处理程序; 提供的参数之一就是一个包含传入电话号码的String

这是一个实现,它将允许您检索电话号码,如果它是来电电话为incomingNumber,也是当呼叫完成时 – 请注意Handler()代码。

 private class PhoneCallListener extends PhoneStateListener { private boolean isPhoneCalling = false; @Override public void onCallStateChanged(int state, String incomingNumber) { if (TelephonyManager.CALL_STATE_RINGING == state) { // phone ringing Log.i(LOG_TAG, "RINGING, number: " + incomingNumber); } if (TelephonyManager.CALL_STATE_OFFHOOK == state) { // active Log.i(LOG_TAG, "OFFHOOK"); isPhoneCalling = true; } if (TelephonyManager.CALL_STATE_IDLE == state) { // run when class initial and phone call ended, need detect flag // from CALL_STATE_OFFHOOK Log.i(LOG_TAG, "IDLE number"); if (isPhoneCalling) { Handler handler = new Handler(); //Put in delay because call log is not updated immediately when state changed // The dialler takes a little bit of time to write to it 500ms seems to be enough handler.postDelayed(new Runnable() { @Override public void run() { // get start of cursor Log.i("CallLogDetailsActivity", "Getting Log activity..."); String[] projection = new String[]{Calls.NUMBER}; Cursor cur = getContentResolver().query(Calls.CONTENT_URI, projection, null, null, Calls.DATE +" desc"); cur.moveToFirst(); String lastCallnumber = cur.getString(0); } },500); isPhoneCalling = false; } } } } 

然后在onCreate或onStartCommand代码中添加并初始化侦听器:

 PhoneCallListener phoneListener = new PhoneCallListener(); TelephonyManager telephonyManager = (TelephonyManager) this .getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE); 

CustomPhoneStateListener重写的方法应该调用onCallStateChanged() (而不是onCallStateChange() )。

如果你有@Override注解,就像Java对onReceive()那样,这会被Java编译器发现。

Interesting Posts