【问题标题】:Android: How to send multiple sms via intent (diff phone no and body)Android:如何通过意图发送多条短信(区分电话号码和正文)
【发布时间】:2014-12-01 11:54:30
【问题描述】:

我正在使用以下代码通过 Intent 发送短信(无法请求许可,因此无法选择 smsmanager)

//Code from this question 
//  <http://stackoverflow.com/questions/20079047/android-kitkat-4-4-hangouts-cannot-handle-sending-sms-intent>
    private void sendsms(String toContact, String text){
        Intent intent;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // Android 4.4 and up
        {
            String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(this);


            intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + Uri.encode(toContact)));
            intent.putExtra("sms_body", text);

            if (defaultSmsPackageName != null) // Can be null in case that there is no default, then the user would be able to choose any app that supports this intent.
            {
                intent.setPackage(defaultSmsPackageName);
            }
        }
        else
        {
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setType("vnd.android-dir/mms-sms");
            intent.putExtra("address", toContact);
            intent.putExtra("sms_body", text);
        }
        this.startActivity(intent);
    }

我在 for 循环中调用它:

for(int i = 0; i<4 ;i++) {
    sendsms(phoneNo[i],smsBody[i]);
   }

现在的问题是,每当用户到达此行时,将调用 4 次 void,但用户只会看到设备默认消息传递应用程序中准备发送的最后一条消息,但要到达其他消息,用户应按回设备,否则,他/她将永远看不到其他消息。

我需要做的是使用像startActivityForResult(); 这样的方法,所以每次用户发送消息时,他都会被重定向到我的应用程序,然后我的应用程序为下一条短信启动另一个活动。

有什么想法吗?

提前致谢

【问题讨论】:

  • smsmanager 是实现这一目标的唯一途径
  • @Nitesh ,你能给我一个来源吗?
  • 您可以通过监听 SMS 内容数据库中的变化来实现相同的目的,当用户通过 SMS 内容解析器发送 SMS 时实现 ContentObserver 并且当您收到回调时,您可以开始编写逻辑以发送另一条 SMS。

标签: android android-intent sms


【解决方案1】:

在for循环中调用这个方法

sendSMS("98********","这是测试短信");

完整方法

/*
* BroadcastReceiver mBrSend; BroadcastReceiver mBrReceive;
*/
private void sendSMS(String phoneNumber, String message) {
ArrayList<PendingIntent> sentPendingIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveredPendingIntents = new ArrayList<PendingIntent>();
PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0,
        new Intent(mContext, SmsSentReceiver.class), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(mContext, 0,
        new Intent(mContext, SmsDeliveredReceiver.class), 0);
try {
    SmsManager sms = SmsManager.getDefault();
    ArrayList<String> mSMSMessage = sms.divideMessage(message);
    for (int i = 0; i < mSMSMessage.size(); i++) {
        sentPendingIntents.add(i, sentPI);
        deliveredPendingIntents.add(i, deliveredPI);
    }
    sms.sendMultipartTextMessage(phoneNumber, null, mSMSMessage,
            sentPendingIntents, deliveredPendingIntents);

} catch (Exception e) {

    e.printStackTrace();
    Toast.makeText(getBaseContext(), "SMS sending failed...",Toast.LENGTH_SHORT).show();
  }

}

现在还有两个类 SmsDeliveredReceiver,SmsSentReceiver 如下。

 public class SmsDeliveredReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent arg1) {
     switch (getResultCode()) {
     case Activity.RESULT_OK:
         Toast.makeText(context, "SMS delivered", Toast.LENGTH_SHORT).show();
     break;
     case Activity.RESULT_CANCELED:
         Toast.makeText(context, "SMS not delivered", Toast.LENGTH_SHORT).show();
     break;
     }
   }

 }

现在是 SMSSentReceiver。

 public class SmsSentReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent arg1) {
     switch (getResultCode()) {
       case Activity.RESULT_OK:
          Toast.makeText(context, "SMS Sent", Toast.LENGTH_SHORT).show();

       break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
    Toast.makeText(context, "SMS generic failure", Toast.LENGTH_SHORT)
            .show();

    break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
    Toast.makeText(context, "SMS no service", Toast.LENGTH_SHORT)
            .show();

    break;
case SmsManager.RESULT_ERROR_NULL_PDU:
    Toast.makeText(context, "SMS null PDU", Toast.LENGTH_SHORT).show();
    break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
    Toast.makeText(context, "SMS radio off", Toast.LENGTH_SHORT).show();
    break;
   }
  }

}

现在权限打开您的 AndroidManifest.xml 并添加以下行

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

它完成了......

【讨论】:

  • 感谢兄弟的努力,但我的意思是一个来源表明这是唯一的方法,没有提到的许可是不可能的。
【解决方案2】:

首先你需要用你的手机创建一个字符串,因为你必须使用“;”作为分隔符,但要小心三星,在那些设备中你必须使用“,”。

所以你的代码必须和这个类似:

private void sendSms(String phone1, String phone2){

    String separator = "; "

    if (Build.MANUFACTURER.toLowerCase().contains("samsung"))
          separator = ", "

    String phones = phone1 + separator + phone2...

    Intent intentSms = new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", phones, null));

    intentSms.putExtra("sms_body","your text") // Just if you want to add text 

    startActivity(intentSms);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 2023-04-02
    • 2013-12-04
    • 1970-01-01
    • 2015-01-04
    • 2019-03-05
    • 1970-01-01
    相关资源
    最近更新 更多