我想检索来电的电话号码并用它做一些事情,就像在http://blog.whitepages.com/2009/02/27/caller-id-by-whitepages-a-new-android-app-使电话推销员处于警报状态/
你能帮我吗,因为我找不到任何关于这个的信息。我从哪里开始以及如何获取电话号码?
好的,所以目前我的代码如下所示。当我拨打电话时,CustomBroadcastReceiver 会捕获它并打印出日志消息。我可以从捆绑包中检索电话号码。但!我无法让 CustomPhoneStateListener 工作。如您所见,我已将我的 customPhoneState 侦听器注册到接收器,但日志消息永远不会从 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);
}