我使用了一个扩展BroadcastReceiver的类。我怎样才能在呼入或呼出电话时得到电话号码?
这是我的部门:
public class MyCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context , Intent intent) {
if (intent.getStringExtra( TelephonyManager.EXTRA_STATE ).equals( TelephonyManager.EXTRA_STATE_OFFHOOK )) {
//Call started
} else if (intent.getStringExtra( TelephonyManager.EXTRA_STATE ).equals( TelephonyManager.EXTRA_STATE_IDLE )) {
//Call ended
} else if (intent.getStringExtra( TelephonyManager.EXTRA_STATE ).equals( TelephonyManager.EXTRA_STATE_RINGING )) {
//Incoming call
}
} }
你可以这样做。
public class CallRecevier extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String phoneNumber) {
super.onCallStateChanged(state, phoneNumber);
if (state == 1 || state == 2) {
if (phoneNumber != null && !phoneNumber.equals("")) {
Tİmber.e("Call Number: %s",phoneNumber);
}
}
}
}, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
Timber.e("Error: %s", e.getLocalizedMessage());
}
}
}