Exemple #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;
  }
  private void sendMessage() {
    AutoCompleteTextView gPerson = (AutoCompleteTextView) findViewById(R.id.contactPerson);
    String person = gPerson.getText().toString();

    Cursor cur =
        managedQuery(
            android.provider.Contacts.People.CONTENT_URI,
            null,
            android.provider.Contacts.PeopleColumns.NAME + "='" + person + "'",
            null);

    while (cur.next()) {
      String phone =
          cur.getString(cur.getColumnIndex(android.provider.Contacts.PhonesColumns.NUMBER));
      person = phone;
    }

    cur.close();

    EditText gMessage = (EditText) findViewById(R.id.textMessage);
    String message = gMessage.getText().toString();

    SmsManager manager = SmsManager.getDefault();
    manager.sendTextMessage(person, null, message, null, null, null);

    // save the sent message for the conversation.
    MessagesDbAdapter gDbAdapter = new MessagesDbAdapter(this);
    gDbAdapter.open();

    gDbAdapter.createMessage(person, message, 1, 1, System.currentTimeMillis());
    gDbAdapter.close();

    Toast.makeText(this, "Text message has been sent.", Toast.LENGTH_LONG).show();
    finish();
  }
  /**
   * Creates a new TextMessage component.
   *
   * @param container ignored (because this is a non-visible component)
   */
  public Texting(ComponentContainer container) {
    super(container.$form());
    Log.d(TAG, "Texting constructor");
    Texting.component = (Texting) this;
    activity = container.$context();

    SharedPreferences prefs = activity.getSharedPreferences(PREF_FILE, Activity.MODE_PRIVATE);
    if (prefs != null) {
      receivingEnabled = prefs.getBoolean(PREF_RCVENABLED, true);
      googleVoiceEnabled = prefs.getBoolean(PREF_GVENABLED, false);
      Log.i(
          TAG,
          "Starting with receiving Enabled="
              + receivingEnabled
              + " GV enabled="
              + googleVoiceEnabled);
    } else {
      receivingEnabled = true;
      googleVoiceEnabled = false;
    }

    smsManager = SmsManager.getDefault();
    PhoneNumber("");

    isInitialized = false; // Set true when the form is initialized and can dispatch
    isRunning = false; // This will be set true in onResume and false in onPause

    // Register this component for lifecycle callbacks
    container.$form().registerForOnInitialize(this);
    container.$form().registerForOnResume(this);
    container.$form().registerForOnPause(this);
    container.$form().registerForOnStop(this);
  }
Exemple #4
0
  /**
   * 后台发短信
   *
   * @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);
      }
    }
  }
  protected void sendSMSMessage() {
    Log.i("Send SMS", "");

    String phoneNo = txtphoneNo.getText().toString();
    String message = txtMessage.getText().toString();

    try {
      SmsManager smsManager = SmsManager.getDefault();
      smsManager.sendTextMessage(phoneNo, null, message, null, null);
      Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
      txtphoneNo.setText("");
      txtMessage.setText("");

    } catch (Exception e) {
      Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG)
          .show();
      e.printStackTrace();
    }
  }