Пример #1
0
  /**
   * 发送短消息(未听取发送和接受成功的广播)
   *
   * @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;
  }
Пример #2
0
  /**
   * 发送短消息,多个收件人
   *
   * @param dests
   * @param content
   * @return
   */
  public boolean sendSms(String[] dests, String content) {
    long threadId =
        Telephony.Threads.getOrCreateThreadId(context, new HashSet<String>(Arrays.asList(dests)));
    if (Toolkit.isBlank(content)) {
      Log.w(TAG, "空的短消息");
      return false;
    }
    if (dests.length == 0) {
      Log.d(TAG, "空的收件人列表");
      return false;
    }
    for (int i = 0; i < dests.length; i++) {
      sendSms(dests[i], content, threadId);
    }

    return true;
  }