为什么PhoneState接收器获得空的传入号码

时间:2018-10-21 07:04:37

标签: android phone-state-listener

如果一个电话号码不在应用程序白名单中并且广播不止一次,并且第一个广播的来电号码为空,则我想阻止传出和传入呼叫,因此该呼叫不会被阻止,并且在大多数情况下都可以正常工作,因为接收方获取传入号码。为什么是空号码?如何处理空广播?

#include <stdio.h>

typedef int *byte_pointer;
void show_bytes(byte_pointer start, size_t len){
int i;
for(i=0; i<len; i++)
    printf(" |%.2x",start[i]);
printf("\n");
}

void show_int(int x){
show_bytes((byte_pointer) &x, sizeof(int));
}

清单

public class CallServiceReciver extends BroadcastReceiver {

private TelephonyManager telephony;
private ITelephony telephonyService;

private Context context;

public void onReceive(final Context context, Intent intent) {


    this.context = context;

    if(!intent.getAction().equals("android.intent.action.PHONE_STATE")){
        return;
    }
    telephony = (TelephonyManager) context.getApplicationContext()
            .getSystemService(Context.TELEPHONY_SERVICE);
    telephony.listen(new TellListener(), PhoneStateListener.LISTEN_CALL_STATE);
    try {
        Class c = Class.forName(telephony.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        telephonyService = (ITelephony) m.invoke(telephony);

    } catch (Exception e) {
        e.printStackTrace();
    }

private class TellListener extends PhoneStateListener {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);

        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:

                if (incomingNumber == null || incomingNumber.equals("")) {
                    return;
                }
                    try {
                        inComingCallAuthorization(((ParentalControl)context.getApplicationContext()).makeCheckPhoneNumber(incomingNumber));
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                break;
            case TelephonyManager.CALL_STATE_IDLE:
                Log.i("anarestan", "Call End" + incomingNumber);

                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                if (incomingNumber == null || incomingNumber.equals("")) {
                    return;
                }
                try {
                    Log.i(TAG, "onCallStateChanged:  offhook");
                    outGoinCallAuthorization(((ParentalControl)context.getApplicationContext()).makeCheckPhoneNumber(incomingNumber));
                } catch (RemoteException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                } catch (Exception e){
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }

        super.onCallStateChanged(state, incomingNumber);

    }
}

1 个答案:

答案 0 :(得分:0)

您不需要这种方法。您需要使用BroadcastReceiver来拦截NEW_OUTGOING_CALL Intent,但是请确保将Intent过滤器的优先级设置为“ -1”。要中止呼出电话,您需要调用“ setResultData(null)”。

更长的解释:这个特定的Intent是“ Ordered”。这意味着此意图尊重您分配给它的“优先级”以对其进行处理。因此,如果您的优先级最高,那么您将是第一个访问/管理它的人,然后更改其值,以使其他意图侦听者使用已更改且不起作用的意图。