Android有来电时如何获取电话号码?
问问题
57621 次
5 回答
41
在Broadcast receiver
ManifestServiceReceiver
中指定其动作。
<receiver android:name=".ServiceReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
添加一个PhoneStateListener
到您的 TelephonyManager,PhoneStateListener 已覆盖onCallStateChanged()
传入号码参数。就是这样。
服务接收器.Java
public class ServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
System.out.println("incomingNumber : "+incomingNumber);
}
},PhoneStateListener.LISTEN_CALL_STATE);
}
}
于 2012-10-31T09:13:10.833 回答
5
以下解决方案帮助我检索传入和传出的电话号码。
清单中要包含的内容:
1) 许可:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
2)广播接收器:
<receiver android:name=".AnswerCallBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
BroadcastReceiver 类中要包含的内容:
public class AnswerCallBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
if(arg1.getAction().equals("android.intent.action.PHONE_STATE")){
String state = arg1.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
Log.d(TAG, "Inside Extra state off hook");
String number = arg1.getStringExtra(TelephonyManager.EXTRA_PHONE_NUMBER);
Log.e(TAG, "outgoing number : " + number);
}
else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
Log.e(TAG, "Inside EXTRA_STATE_RINGING");
String number = arg1.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.e(TAG, "incoming number : " + number);
}
else if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
Log.d(TAG, "Inside EXTRA_STATE_IDLE");
}
}
}
}
于 2014-10-23T05:18:56.803 回答
3
这肯定会对你有所帮助。
这是一个实现,如果它是作为来电号码的来电,以及当呼叫完成时,它允许您检索电话号码 - 请注意 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);
于 2012-10-31T09:16:40.147 回答
2
你可以在这里找到解决方案
Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
于 2012-10-31T09:15:11.093 回答
1
上面的答案是正确的,但是,不要忘记权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Android 9
注意,要读取来电号码的起始表格,您必须请求此权限
<uses-permission android:name="android.permission.READ_CALL_LOG" />
于 2020-05-21T08:45:06.890 回答