无法在android中比较两个字符串

时间:2012-05-25 06:00:21

标签: android string-comparison callblocking

我是Android新手。我正在开发一个阻止不必要的电话的应用程序。现在,当我尝试将收到的号码与接触的号码进行比较时,我就被困在这里。这是代码。请帮忙。 在检查复选框时,我需要阻止来自陌生人的所有来电(不在联系人中)

Bundle extra=intent.getExtras();//new
telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(extra!=null)//new=== getting the blocked number {
    state=extra.getString(telephonyManager.EXTRA_STATE);
    if(state.equals(telephonyManager.EXTRA_STATE_RINGING)) {
        number=extra.getString(telephonyManager.EXTRA_INCOMING_NUMBER);
        Log.w("INCOMMING NUMBER",number);
    }
}
if(noStrangers_cb.isChecked()){
    Cursor phones1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
    while (phones1.moveToNext()){
        String phoneNumber = phones1.getString(phones1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        Log.d("NUMBER IN CONTACT",phoneNumber );

        //boolean val= number== phoneNumber;
        //String no=number;
        //Log.d("ASSIGNING NUMBER TO NO = ", no);
        //Collator c=Collator.getInstance();
        if(!phoneNumber.equals(number))
            {
            Log.d("IF ", "STRANGERS"+number);
            //Log.d("NUMBER CHECKING", "NUMBER = "+number+"CONTACT = "+phoneNumber);
            try {
                telephonyService.endCall();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
    }
    phones1.close();
}//end if Strangers

logcat的: enter image description here

更新后的代码如下:

if(allContacts_cb.isChecked())
                     {
                      Log.d("BLOCK ALL CONTACTS","blocking contacts.........." );
                      Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
                       while (phones.moveToNext())
                       {
                        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        Log.d("NUMBER IN CONTACT ",phoneNumber );
                        String num=phoneNumber.replace("-","");
                        String incom=incomingNumber;
                        String s1="0"+num;
                        String s2="+91"+num;
if((num.equals(incomingNumber))||((s1).equals(incomingNumber))||(s2).equals(incomingNumber))//if(phoneNumber.compareTo(no)==0)//&&(checking==true))
                        { 
                          Log.d("IF ", "INSIDE IF OF BLOCKING CONTACTS");//+cn+"num"+n);
                          Log.d("NUMBER CHECKING", "NUMBER = "+number+"CONTACT = "+phoneNumber);
                          try {
                            ending1=telephonyService.endCall();
                            if(ending1)
                             {  
                             for(int i=1;i<=1;i++)
                             {
                              android.telephony.SmsManager sms = android.telephony.SmsManager.getDefault();
                              sms.sendTextMessage(incom, null, SMS, null, null);
                              break;
                             }
                              }
                              } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                                }

                          } 
                        else
                        {
                            if(noStrangers_cb.isChecked())
                            {
                                try {
                                    telephonyService.endCall();
                                } catch (RemoteException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        }
                          }//end of while
                          phones.close();
                         }//end if allContacts

任何人都可以帮助我..请..

3 个答案:

答案 0 :(得分:2)

您可以在电话号码中将字符串“ - ”替换为“”,然后使用该字符串进行比较。

string no= Phonecontactno.StringReplace("-","");

现在使用此字符串与传入号码进行比较

答案 1 :(得分:2)

从您的号码中替换_字符不是您的最终解决方案。您应该阅读PhoneNumberUtils,或best library 'libphonenumber',它可以用于与电话号码相关的各种类型的操作。

阅读详情about libphonenumber

以下是代码剪切,可用于您的目的。

public boolean isCallerInDatabase(String caller, String phoneNumberFromDb) {
        phoneUtil = PhoneNumberUtil.getInstance();
        if(phoneUtil.isNumberMatch(caller, phoneNumberFromDb) != PhoneNumberUtil.MatchType.NO_MATCH) {
            //Number matched 
            Log.i("TEST", "NUMBER MATCHED");
            return true;
        }
        Log.i("TEST", "NUMBER NOT MATCHED");
        return false;       
    }

更新:Android将该库添加为Utility类。

Android本身已添加PhoneNumberUtils类用于电话相关的帮助方法。要比较两个电话号码,您应使用PhoneNumberUtils.compare方法。

快乐编码:)

答案 2 :(得分:0)

尝试以下条件可能会对您有所帮助

if(!(phoneNumber.toString().equals(number.toString)))