0

我想阻止数据库中的特定电话号码

我在用户拨打的号码和内存中的号码之间进行比较。如果它们相等,我会阻止通话。

我的代码:

public void onReceive(Context context, Intent intent) {

    PlaceDataSQL placeDataSQL =new PlaceDataSQL(context);
    ArrayList<String> getUsersPhoneNumbers= placeDataSQL.getUsersPhoneNumbers(); 
    //TODO
    //===========
    //here I need to check the number

    Bundle b = intent.getExtras();
    String incommingNumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
    //String outGoingNumber = b.getString(TelephonyManager.);
    Boolean find=false;
    try {

        for(int i=0;i<getUsersPhoneNumbers.size();i++)
        {
            if(incommingNumber.equals(getUsersPhoneNumbers.get(i)))
            {
                find=true;
                break;
            }
        }

    } catch (Exception e) {
        incommingNumber="";
    }


// ========================================
//here the problem
//=========================================
    String phonenumber=b.getString(Intent.EXTRA_PHONE_NUMBER);
    try {

        for(int i=0;i<getUsersPhoneNumbers.size();i++)
        {
            if(phonenumber.equals(getUsersPhoneNumbers.get(i)))
            {
                find=true;
                break;
            }
        }
        if (!find)
            return;
    }catch (Exception e) {
        phonenumber="";
    }

    if (!find)
        return;


    /* examine the state of the phone that caused this receiver to fire off */
    String phone_state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    if (phone_state.equals(TelephonyManager.EXTRA_STATE_RINGING)) 
    {

        logMe("Phone Ringing: the phone is ringing, scheduling creation call answer screen activity");
        Intent i = new Intent(context, CallAnswerIntentService.class);
        i.putExtra("delay", 100L);
        i.putExtra("number", incommingNumber);
        context.startService(i);
        logMe("Phone Ringing: started, time to go back to listening");


    }

    if (phone_state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
    {

        Intent i = new Intent(context,InCallScreenGuardService.class);
        i.putExtra("delay", 100L);
        i.putExtra("number", phonenumber);
        logMe("Phone Offhook: starting screen guard service");
        context.startService(i);

    }

    if (phone_state.equals(TelephonyManager.EXTRA_STATE_IDLE))
    {
        Intent i = new Intent(context,InCallScreenGuardService.class);
        logMe("Phone Idle: stopping screen guard service");
        context.stopService(i);

    }

    return;
}

问题:

我可以得到传入的号码,但我不能得到传出的号码?

4

1 回答 1

0

为此,您将需要一个BroadcastReciever

public class OutgoingCallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

        if(null == bundle)
            return;

        String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

        Log.i("OutgoingCallReceiver",phonenumber);
        Log.i("OutgoingCallReceiver",bundle.toString());

        String info = "Detect Calls sample application\nOutgoing number: " + phonenumber;

        Toast.makeText(context, info, Toast.LENGTH_LONG).show();
    }
}
于 2012-06-25T22:48:45.307 回答