PhoneStateListener在某些模型中将空字符串放入incomingNumber - 例如LG-K500ds。 我已在清单文件中给予了许可。
我的代码:
public class MainActivity extends Activity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
telephonyManager.listen(new TelListener(), PhoneStateListener.LISTEN_CALL_STATE);
}
private class TelListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
if(incomingNumber.length()>5)
CurrentPhone = incomingNumber;
}
}
private class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// called when someone is ringing to this phone
TextView tv = (TextView)findViewById(R.id.textView);
tv.setText(incomingNumber);
Toast.makeText(getApplicationContext(),
"Incoming: " + incomingNumber,
Toast.LENGTH_LONG).show();
break;
}
}
}