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

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

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

 public static void sendSms(Context context, String text, List<String> 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个参数是监听短信发送成功失败等状态
发送短信记得添加权限:

<uses-permission android:name="android.permission.SEND_SMS"/>

读取手机联系人信息:

<uses-permission android:name="android.permission.READ_CONTACTS"/>

public static  List<PhoneContact> getAllContactList(Context context) {
        List<PhoneContact> 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;
}

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • JS提供了11种引用类型:Object、Array、Date、Function、Error、RegExp、Math...
    无欲而为阅读 175评论 0 1
  • 面试知识点 1、servlet执行流程 客户端发出http请求,web服务器将请求转发到servlet容器,ser...
    我不是T先生阅读 153评论 1 1
  • 权限管理: 进程安全上下文: 进程对文件的访问权限应用模型: 进程的属主与...
    鹏飞_f673阅读 287评论 0 0
  • Have you Backed Up Your 12-word Seed Phrase? Bitpie remin...
    拜占庭将军阅读 210评论 1 0
  • 我去过很多地方,但我只遇到过很少的我们 有人问我是否喜欢廉江 我说“喜欢啊!” 其实我只是喜欢你 如果那一刻我说我...
    龙猪z阅读 485评论 0 1