unity 获取手机里面联系人

要分别获取安卓和ios的手机联系人

安卓端:

1.首先要unity和安卓的交互,在此不多说,网上很多这样的例子;

2.在xml文件中配置获取权限

3.在MainActivity中写入此段代码

public String GetPhoneContacts()
    {
        //读取联系人权限 
       JSONArray allArray = new JSONArray();
        StringBuffer info = new StringBuffer();
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        ContentResolver resolver = this.getContentResolver();
        //需要查找的信息
        final String[] PHONES_PROJECTION = new String[]
                {
                        ContactsContract.CommonDataKinds.Phone.NUMBER,
                        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                };
        // 获取手机联系人
        Cursor phoneCursor = resolver.query(uri,PHONES_PROJECTION, null, null, null);
        if(phoneCursor!=null)
        {
            while (phoneCursor.moveToNext())
            {
                //得到手机号码
                String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                String nickName = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                if(!TextUtils.isEmpty(phoneNumber))
                {
                    try {
                        JSONObject jsonObject=new JSONObject();
                        jsonObject.put("name",nickName);
                        jsonObject.put("phone",phoneNumber);
                        allArray.put(jsonObject);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
//                    Map map = new HashMap<>();
//                    map.put("name",nickName);
//                    map.put("phone",phoneNumber);
//                    allArray.put(map);
//                    StringBuffer str = new StringBuffer();
//                    str.append("name=");
//                    str.append(nickName);
//                    str.append(",");
//                    str.append("phone=");
//                    str.append(phoneNumber);
//                    allArray.put(str);
//                    info.append("{");
//                    info.append(nickName);
//                    info.append(",");
//                    info.append(phoneNumber);
//                    info.append("}");
                }
            }
            phoneCursor.close();
        }
        // 获取Sims卡联系人
        uri = Uri.parse("content://icc/adn");
        phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null,null);
        if(phoneCursor!=null)
        {
            while (phoneCursor.moveToNext())
            {
                try
                {
                    //得到手机号码
                    String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String nickName = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    if(!TextUtils.isEmpty(phoneNumber))
                    {
                        JSONObject jsonObject=new JSONObject();
                        jsonObject.put("name",nickName);
                        jsonObject.put("phone",phoneNumber);
                        allArray.put(jsonObject);
//                        StringBuffer str = new StringBuffer();
//                        str.append("name=");
//                        str.append(nickName);
//                        str.append(",");
//                        str.append("phone=");
//                        str.append(phoneNumber);
//                        allArray.put(str);
                      /*  Map map = new HashMap<>();
                        map.put("name",nickName);
                        map.put("phone",phoneNumber);
                        allArray.put(map);*/
//                        info.append("{");
//                        info.append(nickName);
//                        info.append(",");
//                        info.append(phoneNumber);
//                        info.append("}");
                    }
                }
                catch(Exception e)
                {
                    break;
                }
            }
            phoneCursor.close();
        }

        uri = Uri.parse("content://sim/adn");
        phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null,null);
        if(phoneCursor!=null)
        {
            while (phoneCursor.moveToNext())
            {
                try
                {
                    //得到手机号码
                    String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String nickName = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    if(!TextUtils.isEmpty(phoneNumber))
                    {

                        JSONObject jsonObject=new JSONObject();
                        jsonObject.put("name",nickName);
                        jsonObject.put("phone",phoneNumber);
                        allArray.put(jsonObject);
//                        StringBuffer str = new StringBuffer();
//                        str.append("name=");
//                        str.append(nickName);
//                        str.append(",");
//                        str.append("phone=");
//                        str.append(phoneNumber);

//                        info.append("{");
//                        info.append(nickName);
//                        info.append(",");
//                        info.append(phoneNumber);
//                        info.append("}");
//                        Map map = new HashMap<>();
//                        map.put("name",nickName);
//                        map.put("phone",phoneNumber);
//                        allArray.put(map);
                    }
                }
                catch(Exception e)
                {
                    break;
                }
            }
            phoneCursor.close();
        }
        return allArray.toString();
    }

4.在unity中只要写

       AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo = jc.GetStatic("currentActivity");
        string phonePerson = jo.Call("GetPhoneContacts");
        this.getPhones = phonePerson;

就可以获取到手机联系人。

数据是json转成string了,也可以转成数组或者其他类型的。

你可能感兴趣的:(Android,unity,C#)