【问题标题】:send multiple messages in android在android中发送多条消息
【发布时间】:2014-01-09 13:13:41
【问题描述】:

我想向五个不同的号码发送消息。我通过使用 getIntent() 方法从另一个页面获取这些数字。任何人都可以请给一个正确的方式来发送消息。我使用了以下代码。但它祝酒“短信发送到第一个号码”,然后所有其他消息都失败了。!!但问题是我什至没有收到任何消息。!!

try{
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage( phonenumber1 ,null,message,null, null);
Toast.makeText(getApplicationContext(), "SMS Sent to First   Number.!",Toast.LENGTH_LONG).show();
}catch(Exception e)
{
Toast.makeText(getApplicationContext(), "SMS Sent failed to First Number.!",Toast.LENGTH_LONG).show();
e.printStackTrace();

} // like this up to phonenumber5 with the same try and catch block each five times

【问题讨论】:

标签: android message


【解决方案1】:

试试这样。首先,将所有数字放入一个字符串数组中,并使用 for 循环而不是 5 try catch。

String receipentsNumber[] = {"111","222","333","444","555"};

for (int i = 0; i < receipentsNumber.length; i++) {

try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(receipentsNumber[i], null, message, null,
                    null);
        Toast.makeText(getApplicationContext(), "SMS Sent to" + " " + receipentsNumber[i], Toast.LENGTH_LONG).show();

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

}

当您获得第一个数字的成功时,我认为您已在清单中添加了权限。

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

希望这会有所帮助。

【讨论】:

【解决方案2】:

5 次尝试捕获不是一个好方法。为什么不把所有数字都放在一个字符串数组中,用for循环代替5 try catch

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多