gpt4 book ai didi

android - 在 android 中检索当前拨出的电话号码时未获得正确的值

转载 作者:太空狗 更新时间:2023-10-29 15:35:04 28 4
gpt4 key购买 nike

我正在使用以下广播接收器来检索拨出的电话号码。

@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
mIntent = intent;
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int events = PhoneStateListener.LISTEN_CALL_STATE;
tm.listen(phoneStateListener, events);
}

private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {



switch (state) {
case TelephonyManager.CALL_STATE_IDLE:

break;

case TelephonyManager.CALL_STATE_OFFHOOK:


dialingNumber = mIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(mContext, "Local Call Number- " + dialingNumber,Toast.LENGTH_SHORT).show();


break;
}
return;
}



Log.i(">>>Broadcast", "onCallStateChanged " + callState);
super.onCallStateChanged(state, incomingNumber);
}
};

有时会返回null或我之前打过的号码,请问有其他方法可以获取当前通话的来电/去电电话号码吗?

最佳答案

对于去电,使用这个:

list :

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

<receiver android:name=".MyPhoneReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>

代码:

...
@Override
public void onReceive(final Context context, final Intent intent) {
Log.d("APP", "ACTION:" + intent.getAction());

if (Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) {
final String originalNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.d("APP", "outgoing,ringing:" + originalNumber);
}
...

对于来电,使用这个:

list :

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

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

代码:

@Override
public void onReceive(final Context context, final Intent intent) {
Log.d("APP", "ACTION:" + intent.getAction());
final String stringExtra = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (TelephonyManager.EXTRA_STATE_RINGING.equals(stringExtra)) {
final String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.d("APP", "incoming,ringing:" + incomingNumber);
} else if (stringExtra.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
final String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.d("APP", "hanged" + incomingNumber );
} else if (stringExtra.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
final String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.d("APP", "answered" + incomingNumber );
}
}

关于android - 在 android 中检索当前拨出的电话号码时未获得正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15473508/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com