English 中文(简体)
无法比较两个字符串和机器人字符串
原标题:Not able to compare two string in android

I m new to Android. I m developing an app which blocks unwanted calls. Now I m stuck here when I try to compare the incoming number with the numbers in contact. Here is the code. Please help. Here while checking a Checkbox I need to block all calls from strangers (not in the contact)

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

任何人都可以帮助我.. 求你了..

最佳回答

从您的号码中替换字符不是最后的解决办法。 您应该读到 < a href=" http:// developmenter. android. com/ reference/ android/ telephony/ Phone NumberUtils. html" rel= “ nofol” > Phone InnumberUtils , 或者这里是 < a href=> http://code.google. com/ p/libone序号/" rel="nofollown" > 图书馆最好的libphone序号 , 可用于与电话号码有关的各种操作。

阅读详情,“http://code.google.com/p/libianphonenumber/"rel=“no follow”关于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 添加该库为实用类 。

Android 本身已被添加到 andref="http:// developtrader.android.com/ reference/android/croid/telephony/PhoneNemberUtils.html" rel="nofollow" >PhoneNemberUtils 类,用于电话相关辅助器的方法。要比较两个电话号码,您应该使用 < a href=""http://developtrader.android.android.com/android/telephony/PhoneNationUtils.html#compare%28java.lang.String,%20java.lang.string%29" rel="nofolpol>>Phone InternUtils.compare 方法。

快乐编码 :)

问题回答

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

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

现在使用此字符串来比较输入的否 。

尝试以下条件也许对您有帮助

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




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签