Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓:同步发送短信_Android_Sms_Broadcastreceiver - Fatal编程技术网

Android 安卓:同步发送短信

Android 安卓:同步发送短信,android,sms,broadcastreceiver,Android,Sms,Broadcastreceiver,我指的是一篇从Android发送短信的文章 我试着用循环发送10条短信给10个不同的号码。 e、 g.通过发送短信(电话号码、信息)进行循环 但在发送6条短信后,sms.sendTextMessage(电话号码、空、短信、sentPI、deliveredPI)提供NullPointerExection 仅供参考,我在deliveredPI中传递空值,因为我不关心sms的传递。但知道短信是否真的被发送是非常重要的,这可以通过注册广播接收器来检索 我注册了一个广播接收器,但整个过程似乎并没有同步。我

我指的是一篇从Android发送短信的文章

我试着用循环发送10条短信给10个不同的号码。 e、 g.通过
发送短信(电话号码、信息)进行循环
但在发送6条短信后,
sms.sendTextMessage(电话号码、空、短信、sentPI、deliveredPI)提供NullPointerExection

仅供参考,我在deliveredPI中传递空值,因为我不关心sms的传递。但知道短信是否真的被发送是非常重要的,这可以通过注册广播接收器来检索

我注册了一个广播接收器,但整个过程似乎并没有同步。我们应该只在收到前一条短信的状态后才发送第二条短信

我假设降低发送sms的速度将有助于删除NullPointerExection。等待状态将是保持发送另一条短信之间的时间间隔的最好办法


简而言之,我想做->发送一条短信->等待状态->更新数据库中的状态->发送另一条短信。

我个人还没有测试我发布的代码,但它被接受为答案,所以我认为它会起作用

    SmsManager smsMan = new SmsManager.getDefault();
    ArrayList<String> contactList = new ArrayList();
    //add contacts to contactList with contactList.add(string)
    for (int i = 0; i <= contactList().size(); i++) {
    String SENT = contactList.get(i).toString();// you could replace this with i,
    //or something like "sms_sent_myappname" + i.toString());

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
            new Intent(SENT, 0);

        //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off", 
                            Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        }, new IntentFilter(SENT));
smsManager.sendTextMessage(contactList.get(i).toString(), null, message, sentPI, null);
}

再说一遍。我还没有测试过,但应该可以用。如果您还有任何问题或者我对任何事情都不清楚,请告诉我。

我将我的评论作为答案,因为我实际上无法评论。
Jakar,你提供的解决方案行不通。您没有在任何地方注销广播接收器。您的代码将出现错误。

我想您已经找到了答案。但既然问题还在这里。。。它会使用sendMultipartTextMessage()吗?使用sendTextMessage()时有一个等于160个符号的限制,这是标准的单个SMS消息的最大长度。

您提供的代码会导致递归,我担心它的影响。不过,我会实现它,如果有的话,在这里发布..@Vikas,我很抱歉。我在最初的帖子中把代码搞乱了。我现在已经更新了第一个块和第二个块,这样代码就可以了。我希望这会对你有更多帮助。谢谢&没关系,我在第一篇文章中就明白了你的意思。我尝试了那个代码,但也遇到了类似的问题。在发送了6条短信后,我的服务崩溃了。。。我正在尝试创建新的项目(删除额外的代码),并尝试为您提供一个zip,以便您可以使用它。。
public void onCreate(Bundle savedInstanceState) {
smsMan = new SmsManager.getDefault(); //assuming you declared SmsManager smsMan in class body.
contactList = new ArrayList(); //assuming you declared ArrayList<String> contactList in class body.
//add contacts to contactList with contactList.add(string);
}
public void sendSms(int position){
    //add contacts to contactList with contactList.add(string)
    String SENT = contactList.get(position).toString();// you could replace this with i,
    //or something like "sms_sent_myappname" + i.toString());

        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
                new Intent(SENT, 0);

            //---when the SMS has been sent---
            registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode())
                    {
                        case Activity.RESULT_OK
                        context.unregisterReceiver(this);
                        i++;
                        if (contactList.size()<i){
                            sendSms(i);
                        } else {
                            //You are done sending - Do what you want.
                        }
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            }, new IntentFilter(SENT));
    smsManager.sendTextMessage(contactList.get(position).toString(), null, message, sentPI, null);

}