在联系人选取器中获取不正确的联系人号码 - Android

Posted

技术标签:

【中文标题】在联系人选取器中获取不正确的联系人号码 - Android【英文标题】:Incorrect Contact Number Fetching in Contact Picker - Android 【发布时间】:2018-01-09 07:59:51 【问题描述】:

我正在使用联系人选择器将号码发送到 editText。根据用户保存特定号码的方式,保存的联系人号码可以分为三种类型。

    +911234567899(前缀为+91)[印度国家代码]。 01234567899(以 0 为前缀)[用于拨打手机号码的常用 STD 码]。 1234567899(无前缀)。

我需要删除前缀(如果有)并在我的 editText 中获取不带空格的实际 10 位数字(一些设备在数字之间放置空格以提高可读性)。

现在我面临的问题是,这仅适用于您第一次选择每种类型的联系人时,例如,它适用于您第一次选择前缀号码和非前缀号码。下次你尝试它时,它不会像我需要的那样工作。但是如果我们重新启动应用程序,它会再次完美运行。

这是代码:

ScreenMain.java

import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class ScreenMain extends AppCompatActivity 


    private static final int PERMISSION_ALL = 1;

    ImageButton btnGetNumber;

    EditText etMobileNumber;

    private String sMobileNumber = "";

    private Uri uriContact;
    private String contactID;     // contacts unique ID


    private static ScreenMain inst;

    public static ScreenMain instance() 
        return inst;
    


    @Override
    protected void onCreate(Bundle savedInstanceState)
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_main);

        btnGetData = (Button) findViewById(R.id.btndGetStatus);

        etMobileNumber = (EditText) findViewById(R.id.etdMobileNumber);


        String[] PERMISSIONS =     Manifest.permission.READ_CONTACTS,
                                    Manifest.permission.WRITE_CONTACTS,
                                    ;

        ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);


        etMobileNumber.setOnClickListener(new View.OnClickListener()
        
            public void onClick(View view)
            
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

            
        );

        btnGetNumber.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                // using native contacts selection
                // Intent.ACTION_PICK = Pick an item from the data, returning what was selected.


                startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), REQUEST_CODE_PICK_CONTACTS);
            
        );

        btnGetData.setOnClickListener(new View.OnClickListener() 
            public void onClick(View view) 
                // sendSms("mONST");
                ClassSMS.sendSms(getApplicationContext(),"mONST",etMobileNumber.getText().toString());
            
        );


        btnCall.setOnClickListener(new View.OnClickListener() 
            public void onClick(View view) 

              //  ClassMakeCall.makeCall(getApplicationContext(),etMobileNumber.getText().toString());

                String ssMobNumber = etMobileNumber.getText().toString();
                if (ssMobNumber.length() == 0) 
                    Toast.makeText(getApplicationContext(), " Please enter mobile number in the motorON unit", Toast.LENGTH_SHORT).show();
                    return;
                
                if (ssMobNumber.length() != 10) 
                    Toast.makeText(getApplicationContext(), " Invalid mobile number", Toast.LENGTH_SHORT).show();
                    return;
                

                try 
                    Intent callIntent = new Intent(Intent.ACTION_CALL);
                    callIntent.setData(Uri.parse("tel:+91" + ssMobNumber));
                    if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) 
                        // TODO: Consider calling
                        //    ActivityCompat#requestPermissions
                        // here to request the missing permissions, and then overriding
                        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                        //                                          int[] grantResults)
                        // to handle the case where the user grants the permission. See the documentation
                        // for ActivityCompat#requestPermissions for more details.

                        Toast.makeText(getApplicationContext()," SMS Fail, Please goto Settings -> APPS -> motorON -> Permissions and enable SMS permission ", Toast.LENGTH_SHORT).show();
                        return;
                    
                   startActivity(callIntent);
                 catch (ActivityNotFoundException activityException) 
                    Toast.makeText(getApplicationContext()," unable to make call", Toast.LENGTH_SHORT).show();
                


            
        );

        ivSettings.setOnClickListener( new View.OnClickListener() 

            @Override
            public void onClick(View v) 
                startActivity(new Intent(getApplicationContext(), ScreenSettings.class));
            
        );


    

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE_PICK_CONTACTS && resultCode == RESULT_OK) 
            //   Log.d(TAG, "Response: " + data.toString());
            uriContact = data.getData();


            ClassGetPhoneNumber.retrieveContactNumber(getApplicationContext(),uriContact);
            if(ClassGetPhoneNumber.sStatus.equals("OK"))
            
                etMobileNumber.setText(ClassGetPhoneNumber.sResponse);

            

        
    


    @Override
    protected void onStart() 
        super.onStart();
        inst = this;
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    

    @Override
    protected void onRestart()
        super.onRestart();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        //  Log.d(TAG, "atvt restart");
    

    @Override
    protected void onResume()
        super.onResume();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        //   Log.d(TAG, "atvt resume");

    

    @Override
    protected void onPause()
        super.onPause();

        //   Log.d(TAG, "atvt pause");
    

    @Override
    protected void onStop()
        super.onStop();

        //  Log.d(TAG, "atvt stop");
    

    @Override
    protected void onDestroy()
    
        super.onDestroy();
        //   Log.d(TAG, "atvt destroy");
    



    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults)
    
        switch (requestCode)
        
            case PERMISSION_ALL:
                
                    // If request is cancelled, the result arrays are empty.
                    if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                    
                    
                    else
                    
                        Toast.makeText(getApplicationContext()," App Needs SMS Permission to Work ", Toast.LENGTH_SHORT).show();
                    
                    return;
                
        
    




    

ClassGetPhoneNumber.java

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Log;

class ClassGetPhoneNumber

    private static final String TAG = null ;
    public static String sStatus="ERROR";
    public static String sResponse;
    public static String sContactNumber;
    public static String sContactNumberFinal;
    public static int With91 = 0, With0 = 0, TenDigit = 0, InvalidNumber = 0;
    public static String contactID="";     // contacts unique ID
    public static String contactNumber = null;

    public static void retrieveContactNumber(Context context, Uri uriContact)
    
        // getting contacts ID
        Cursor cursorID = context.getContentResolver().query(uriContact,
                new String[]ContactsContract.Contacts._ID,
                null, null, null);

        if (cursorID.moveToFirst()) 

            contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
        

        cursorID.close();

        // Using the contact ID now we will get contact phone number
        Cursor cursorPhone = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                new String[]ContactsContract.CommonDataKinds.Phone.NUMBER,

                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
                        ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
                        ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,

                new String[]contactID,
                null);

        contactNumber = null;

        if (cursorPhone.moveToFirst()) 
            contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        

        cursorPhone.close();

        sResponse = null;
        sContactNumber = null;
        sContactNumberFinal = null;

        sContactNumber = contactNumber.replaceAll("\\s","");
        int iLength = sContactNumber.length();

        if(iLength==13)
            With91 = 1;
        if(iLength==11)
            With0 = 1;
        if(iLength==10)
            TenDigit = 1;

        if(With91 == 1)
        
                sContactNumberFinal = sContactNumber.substring(3);
                Log.e(TAG,"In With91, sContactNumberFinal : "+sContactNumberFinal);
                sResponse = sContactNumberFinal;
                sStatus = "OK";
        

        if (With0 == 1)
        
                sContactNumberFinal = sContactNumber.substring(1);
                Log.e(TAG,"In With0, sContactNumberFinal : "+sContactNumberFinal);
                sResponse = sContactNumberFinal;
                sStatus = "OK";
        

        if (TenDigit == 1)
        
                sContactNumberFinal = sContactNumber;
                Log.e(TAG,"In TenDigit, sContactNumberFinal : "+sContactNumberFinal);
                sResponse = sContactNumberFinal;
                sStatus = "OK";
        

    

【问题讨论】:

两个通用 cmets - 您正在使用正则表达式来删除所有空格 (contactNumber.replaceAll("\\s","")) - 为什么不这样做来删除前导 0(类似于:contactNumber.replaceAll("^0", ""))和 + 91?这将大大简化 retrieveContactNumber 方法中的代码。再想一想 - 您似乎很乐意将其仅限于印度电话号码 (+91),但不要忘记其他国家/地区有不同的代码和不同的格式。 【参考方案1】:

我同意@DaveyDaveDave 静态字段不是要走的路,但如果你坚持这里是你的相同代码,有很多修复和简化的代码。 请注意,仅 API 16 及更高版本支持 NORMALIZED_NUMBER。

class ClassGetPhoneNumber 

    ...

    public static void retrieveContactNumber(Context context, Uri uriContact) 
        sResponse = null;
        sContactNumber = null;
        sContactNumberFinal = null;

        // get contactID from contactUri
        long contactID = ContentUris.parseId(contactUri);

        // Using the contact ID now we will get contact phone number
        // Don't add selection by Phone.TYPE as we might miss a lot of phones.
        // We ask for the normalized number, this will only work for API 16 and above
        Cursor cursorPhone = context.getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI,
                new String[]CommonDataKinds.Phone.NORMALIZED_NUMBER, CommonDataKinds.Phone.NUMBER,
                CommonDataKinds.Phone.CONTACT_ID + " = " + contactID,null,null);

        contactNumber = null;

        if (cursorPhone != null && cursorPhone.moveToFirst()) 
            contactNumber = cursorPhone.getString(0); // this number will always be of e164 format: "+<country><local number>"
            Log.d(TAG, "normalized number: " + contactNumber + ", original number: " + cursorPhone.getString(1) + ", contact-id: " + contactID);
         else 
            // you need to quit here, otherwise you'll be using an old value of contactNumber in the rest of the code.
            if (cursorPhone != null) 
                cursorPhone.close();
            
            return; 
        

        cursorPhone.close();

        if (!TextUtils.isEmpty(contactNumber)) 
            sContactNumber = contactNumber;
            sContactNumberFinal = sContactNumber.substring(3);
            sResponse = sContactNumberFinal;
            sStatus = "OK";
         else 
            // no phone was found
            sStatus = "NOT FOUND";
        
    

【讨论】:

感谢@marmor 提出并解决我的问题。这个工作正常,我添加了 sn-p 以删除 +91 和 0,而没有使用冗长的 if_statement 来达到我的目的。 很好,如果您确实需要支持 API 15 及更低版本,或者有时从 NORMALIZED_NUMBER 中获取 null,请查看 Google 的 libphonenumber 实用程序以处理和格式化电话号码:github.com/googlei18n/libphonenumber【参考方案2】:

很难遵循您的代码,因为发生了很多事情,但我认为根本原因是您的 ClassGetPhoneNumber 类中的所有内容都是静态的,因此该类中的字段是在第一遍设置的,并且它们的值在您下次调用 retrieveContactNumber 方法时保持不变。

从观察来看,我怀疑它并不像第一次工作并且随后不工作那么简单,而是您通常会得到“奇怪”的行为,具体取决于输入。

最简单的解决方案是从ClassGetPhoneNumber 类中删除所有static 关键字,并将调用更改为:

ClassGetPhoneNumber.retrieveContactNumber(getApplicationContext(),uriContact);

到:

new ClassGetPhoneNumber().retrieveContactNumber(getApplicationContext(),uriContact);

我认为这将解决您面临的直接问题。

【讨论】:

【参考方案3】:

我想通了。

问题是: 我用来确定用户如何保存的变量的值 特定数字(With91、With0、TenDigit)在开始时未初始化为零 retrieveContactNumber() 方法。 在函数开始时将它们初始化为零清除了之前的 值并使其按预期工作。

感谢大家的时间和支持。 ——保罗·瓦尔盖斯

【讨论】:

嘿,保罗,您的代码中似乎还有其他问题:(1)您不能假设手机的类型为“MOBILE”,它可能设置为其他标签(MAIN、CUSTOM , PRIVATE, WORK),您应该从选择中删除它。 (2) 您可以通过long contactId = ContentUris.parseId(contactUri); (3) 代替正则表达式和解析电话号码,使用 CommonDataKinds.Phone.NORMALIZED_NUMBER 始终获得相同的类型 你能用我的代码解释一下吗?这会很有帮助。 是的,我添加了一个固定版本作为新答案,请注意我所有的 cmets 内联

以上是关于在联系人选取器中获取不正确的联系人号码 - Android的主要内容,如果未能解决你的问题,请参考以下文章

android获取联系人图片、姓名和手机号码

多个联系人选取器列表

如何通过电话号码获取联系人ID

Windows phone 8.1 使用联系人选取器检索电子邮件和电话号码

使用 ContentProvider 从联系人获取电话号码 - Android

在 Android 上通过电话号码获取联系人