android短信群发多个联系人,联系人怎么分开,以及读取联系人

群发短信(不是直接发送,只是调起系统短信发送页面,联系人自动填入)

这里面注意的就是symbol分隔符,目前测试都已经适配(也反编译过CM的WhatsCall产品)

 public static void sendSms(Context context, String text, List numbers) {
        String numbersStr = "";
        String symbol = "Samsung".equalsIgnoreCase(Build.MANUFACTURER) ? ",": ";";
        if (numbers != null && !numbers.isEmpty()) {
            numbersStr = TextUtils.join(symbol, numbers);
        }
        Uri uri = Uri.parse("smsto:" + numbersStr);

        Intent intent = new Intent();
        intent.setData(uri);
        intent.putExtra("sms_body", text);
        intent.setAction(Intent.ACTION_SENDTO);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(context);
            if (defaultSmsPackageName != null) {
                intent.setPackage(defaultSmsPackageName);
            }
        }
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        try {
            context.startActivity(intent);
        } catch (Exception e) {
        }
}

上面代码是不需要任何权限的!

如果要做短信群发可以使用:

smsManager.sendTextMessage(etNumber.getText().toString(), null,etMessage.getText().toString(), null, null);

后面2个参数是监听短信发送成功失败等状态
发送短信记得添加权限:


读取手机联系人信息:



public static  List getAllContactList(Context context) {
        List smsContactList = new ArrayList<>();
        try {
            Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME}
                    , null, null, ContactsContract.CommonDataKinds.Phone.SORT_KEY_PRIMARY);

            if (cursor != null && cursor.getCount() > 0) {
                String lastPhone = null;
                while (cursor.moveToNext()) {
                    PhoneContact smsContact = new PhoneContact();
                    smsContact.name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    smsContact.contactId = cursor.getLong(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));

                    String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    if (!TextUtils.isEmpty(phone)) {
                        //安卓本身的电话获取出来是经过格式化的, 不是连续的数字
                        phone = formatNum(phone);
                        smsContact.number = phone;
                        if (TextUtils.isEmpty(phone) || TextUtils.equals(lastPhone, phone)) {
                            continue;
                        }
                        lastPhone = phone;
                    }
                    smsContactList.add(smsContact);
                }
            }


            if (cursor != null) {
                try {
                    cursor.close();
                } catch (Throwable throwable) {
                  
                }
            }
        } catch (Exception e) {
          
        }

        return smsContactList;
    }

    private static String formatNum(String phone) {
        if (phone != null) {
            char x20 = 0x20;
            char xa0 = 0xa0;
            phone = phone.replace(x20, xa0);
            phone = phone.replace(" ", "");
            phone = phone.replace(" ", "");
            phone = phone.replace("-", "");
        }
        return phone;
    }

关于检查某些权限是否授权:

(这是编译的targetSDK导致,可以如下代码处理!但是发现有些华为,小米手机,去设置页面关闭权限,下面代码还是不好使,知道具体怎么解决的,可以留言告知谢谢!)

int targetSdkVersion = 0;
try {
    final PackageInfo info = context.getPackageManager().getPackageInfo(
            context.getPackageName(), 0);
    targetSdkVersion = info.applicationInfo.targetSdkVersion;
} catch (Exception e) {
}

boolean isGrant = false;
if (targetSdkVersion >= Build.VERSION_CODES.M){
    isGrant = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED;
}else{
    isGrant = PermissionChecker.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) == PermissionChecker.PERMISSION_GRANTED;
}

--------- 记得点喜欢支持,你们的支持是我写作的动力,谢谢!---------

你可能感兴趣的:(android短信群发多个联系人,联系人怎么分开,以及读取联系人)