/** * 发送短消息(未听取发送和接受成功的广播) * * @param number 号码 * @param content 内容 * @return 是否成功 */ public boolean sendSms(String dest, String content, long threadId) { SmsManager manager = SmsManager.getDefault(); HashSet<String> recipients = new HashSet<String>(); recipients.add(dest); threadId = threadId > 0 ? threadId : Telephony.Threads.getOrCreateThreadId(context, recipients); if (Toolkit.isBlank(dest) || Toolkit.isBlank(content)) { Log.w(TAG, "空的联系人或短消息"); return false; } ArrayList<String> parts = manager.divideMessage(content); int msgCount = parts.size(); ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(msgCount); // 写入发件箱 Uri uri = Sms.Outbox.addMessage( context.getContentResolver(), dest, content, null, System.currentTimeMillis(), false, threadId); for (int i = 0; i < msgCount; i++) { PendingIntent sentIntent = PendingIntent.getBroadcast( context, 0, new Intent(MESSAGE_SENT_ACTION, uri, context, SmsReceiver.class), 0); sentIntents.add(sentIntent); } manager.sendMultipartTextMessage(dest, null, parts, sentIntents, null); return true; }
/** * 后台发短信 * * @param message * @param Number */ public static void sendSmsBackground(String message, String Number) { // 移动运营商允许每次发送的字节数据有限,我们可以使用Android给我们提供 的短信工具。 if (message != null) { SmsManager sms = SmsManager.getDefault(); // 如果短信没有超过限制长度,则返回一个长度的List。 List<String> texts = sms.divideMessage(message); for (String text : texts) { sms.sendTextMessage(Number, MessagCenterPhoneNumber, message, null, null); // sms.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent); } } }