如何从设备获取联系电话?

Posted

技术标签:

【中文标题】如何从设备获取联系电话?【英文标题】:How do I get contact numbers from device? 【发布时间】:2016-12-12 16:16:32 【问题描述】:

我想在ListView 中显示联系电话。它运行良好。但是我想在从设备读取联系人时设置条件。条件是

    if number digit <10 --don't add into list-.

    else if(number is started from 0 then replace with 91)--Add to list.

    else ifnumber is start from + then remove it --add to list。

    else其他都是直接加入列表。

这是我的代码:

public class ReferFriend extends AppCompatActivity 

    ArrayList<SelectUser> selectUsers;
    List<SelectUser> temp;

    ListView listView;

    Cursor phones, email;


    ContentResolver resolver;
    SearchView search;
    SelectUserAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.refer_friend);

        selectUsers = new ArrayList<SelectUser>();
        resolver = this.getContentResolver();
        listView = (ListView) findViewById(R.id.contacts_list);

        phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
        LoadContact loadContact = new LoadContact();
        loadContact.execute();
    

    // Load data on background
    class LoadContact extends AsyncTask<Void, Void, Void> 
        @Override
        protected void onPreExecute() 
            super.onPreExecute();

        

        @Override
        protected Void doInBackground(Void... voids) 
            // Get Contact list from Phone

            if (phones != null) 
                Log.e("count", "" + phones.getCount());
                if (phones.getCount() == 0) 
                    Toast.makeText(ReferFriend.this, "No contacts in your contact list.", Toast.LENGTH_LONG).show();
                

                while (phones.moveToNext()) 
                    Bitmap bit_thumb = null;
                    String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                    String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String image_thumb = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI));
                    try 
                        if (image_thumb != null) 
                            bit_thumb = MediaStore.Images.Media.getBitmap(resolver, Uri.parse(image_thumb));
                         else 
                            Log.e("No Image Thumb", "--------------");
                        
                     catch (IOException e) 
                        e.printStackTrace();
                    

                    SelectUser selectUser = new SelectUser();
                    selectUser.setThumb(bit_thumb);
                    selectUser.setContactName(name);
                    selectUser.setPhone(phoneNumber);
                    selectUser.setContactEmail(id);
                    selectUser.setCheckedBox(false);
                    selectUsers.add(selectUser);
                
             else 
                Log.e("Cursor close 1", "----------------");
            
            //phones.close();
            return null;
        

        @Override
        protected void onPostExecute(Void aVoid) 
            super.onPostExecute(aVoid);
            adapter = new SelectUserAdapter(selectUsers, ReferFriend.this);
            listView.setAdapter(adapter);

            // Select item on listclick
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) 

                    Log.e("search", "here---------------- listener");

                    SelectUser data = selectUsers.get(i);
                
            );

            listView.setFastScrollEnabled(true);
        
    

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

    public class SelectUserAdapter extends BaseAdapter 

        public List<SelectUser> _data;
        private ArrayList<SelectUser> arraylist;
        Context _c;
        ViewHolder v;
        RoundImage roundedImage;

        public SelectUserAdapter(List<SelectUser> selectUsers, Context context) 
            _data = selectUsers;
            _c = context;
            this.arraylist = new ArrayList<SelectUser>();
            this.arraylist.addAll(_data);
        

        @Override
        public int getCount() 
            return _data.size();
        

        @Override
        public Object getItem(int i) 
            return _data.get(i);
        

        @Override
        public long getItemId(int i) 
            return i;
        

        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) 
            View view = convertView;
            if (view == null) 
                LayoutInflater li = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = li.inflate(R.layout.contact_info, null);
                Log.e("Inside", "here--------------------------- In view1");
             else 
                view = convertView;
                Log.e("Inside", "here--------------------------- In view2");
            

            v = new ViewHolder();

            v.title = (TextView) view.findViewById(R.id.name);
            v.check = (CheckBox) view.findViewById(R.id.check);
            v.phone = (TextView) view.findViewById(R.id.no);
            v.imageView = (ImageView) view.findViewById(R.id.pic);

            final SelectUser data = (SelectUser) _data.get(i);
            v.title.setText(data.getContactName());
            v.check.setChecked(data.getCheckedBox());
            //v.phone.setText(data.getPhone());

            //I want to apply that condition here

            /*if (data.getPhone().length() <= 10) 
                v.phone.setText("Contact not Added to list");
             else if(data.getPhone().length() == 10)
                v.phone.setText(data.getPhone());
             else if(data.getPhone().equalsIgnoreCase("+"))
                v.phone.setText(data.getPhone());
            else if(data.getPhone().startsWith("0"))
                v.phone.setText(data.getPhone().replace(0,91));
            */

            view.setTag(data);
            return view;
        

        // Filter Class
        public void filter(String charText) 
            charText = charText.toLowerCase(Locale.getDefault());
            _data.clear();
            if (charText.length() == 0) 
                _data.addAll(arraylist);
             else 
                for (SelectUser wp : arraylist) 
                    if (wp.getContactName().toLowerCase(Locale.getDefault())
                            .contains(charText)) 
                        _data.add(wp);
                    
                
            
            notifyDataSetChanged();
        
        class ViewHolder 
            ImageView imageView;
            TextView title, phone;
            CheckBox check;
        
    

【问题讨论】:

这个问题能回答你的问题吗? ***.com/questions/15243278/… 谢谢你解决它.. 如果上面的链接对你有用,请接受它作为重复。 【参考方案1】:

在添加联系人记录验证功能之前,它将验证您所有需要的条件。如果验证成功,则该函数将返回 true,否则返回 false。如果返回 true,则将该联系人添加到 selectUser 对象,否则不要添加。

【讨论】:

我已经添加了一些条件,但是它们没有以正确的方式编写,代码在我的帖子中被注释块。请检查一下并给我解决方案【参考方案2】:
SelectUser selectUser = new SelectUser();
            selectUser.setThumb(bit_thumb);
            selectUser.setContactName(name);
            selectUser.setPhone(phoneNumber);
            selectUser.setContactEmail(id);
            selectUser.setCheckedBox(false);


            //object preparation
            Boolean flag = prepareObject(selectUser);

            if(flag)
            //add valid object to arraylist
            selectUsers.add(selectUser);
            else
            //invalid contact details
            

            //method to validate and update object
            public Boolean prepareObject(SelectUser obj)

                if(obj.getPhone.lenght < 10) 
                    //dont add to list
                else

                    String temp = obj.getPhone.charAt(0);
                    if(temp == "0")

                        //replace it with 91 and update object 

                        return true
                    else if(temp == "+")

                        //remove it and update object
                        return true

                    else

                    //add to object
                    return true
                    

                


                return false;
            

我已经为您的问题写下了逻辑代码。只需对其进行必要的更改,它肯定会起作用。

您直接将对象添加到 arraylist 而不检查您的条件。 您在适配器上编写错误的条件,apdater 只会从 arraylist 填充数据。如果您将有效数据放入 arraylist,则无需在适配器上设置条件。

【讨论】:

好的@Dnyanesh 我会试试这个。谢谢 这一行出现错误 obj.getPhone.lenght

以上是关于如何从设备获取联系电话?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 phonegap 作为复选框列表显示从 android 设备获取的联系人?

Android以编程方式从联系人获取设备默认电话号码

如何从电话簿中获取与 android 中特定联系人相关联的所有社交帐户?

从 android 设备获取超过 10000 个联系人

如何以编程方式从 iOS 设备获取自己的手机号码? [复制]

如何从地址簿获取联系人到我们的本机应用程序