我如何获得的电话号码时,有在Android的来电?
Answer 1:
做一个Broadcast receiver
说ServiceReceiver
转让其在清单行动。
<receiver android:name=".ServiceReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
a添加PhoneStateListener
您TelephonyManager,其覆盖PhoneStateListener onCallStateChanged()
与来电号码参数。 而已。
ServiceReceiver.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);
}
}
Answer 2:
下面的解决方案帮我找回传入和传出的电话号码。
事情的清单包括 :
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>
事情在广播接收器类包括 :
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");
}
}
}
}
Answer 3:
请参考这个问题: 在Android中检索来电的电话号码
也看看这个答案太: 我如何才能从来电的电话号码?
Answer 4:
你可以找到解决方案在这里
Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
Answer 5:
通过使用公认的答案,你会注册一个新的监听与通话状态的每一次变化。 这将导致对同一事件的多个onCallStateChanged电话。
Answer 6:
这一定会帮助你。
这里是一个实现,这将允许您检索电话号码,如果它是一个呼入电话的来电号码,也可当通话结束 - 注意处理程序()的代码。
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);
文章来源: How to get phone number from an incoming call?