我想在android中获取传入的电话号码。我尝试使用BroadcastListener,我的Broadcastlistener看起来像这样。
public class InterceptCall extends BroadcastReceiver { public int cnt = 0; public int cnt2 = 0; TelephonyManager telephony; @Override public void onReceive(final Context context, final Intent intent) { if(telephony != null)return; telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); telephony.listen(new PhoneStateListener() { @Override public void onCallStateChanged(int state, String incomingNumber) { String state1 = intent.getStringExtra(TelephonyManager.EXTRA_STATE); if (state1.equals(TelephonyManager.EXTRA_STATE_RINGING) && state == 1) { Toast.makeText(context, "Ringing - " + cnt, Toast.LENGTH_SHORT).show(); cnt++; System.out.println("incomingNumber : " + cnt + " " + incomingNumber); } } }, PhoneStateListener.LISTEN_CALL_STATE); } }
这可以正确提供号码,但问题是当任何新呼叫都响起时,总是显示在吐司中的先前号码。例如,如果三个呼叫接连出现,则第三次将有3个Toast消息。我该如何解决?
我想在android中获取传入的电话号码。我尝试使用BroadcastListener,我的Broadcastlistener看起来像这样。.公共类InterceptCall扩展了BroadcastReceiver {...
我认为您正在混合使用两种不同的方法来获取呼叫状态更改的更新:BroadcastReceiver
和PhoneStateListener
。