通过(SmsManager)在Android中发送问题和延迟的问题报告

时间:2019-04-04 02:00:49

标签: java android smsmanager

我正在构建一个应用程序,该应用程序从电话发送消息,并报告成功和失败消息的数量。 邮件已创建,但邮件已延迟,我无法获得确切的报告。

String SENT = "SMS_SENT", DELIVERED = "SMS_DELIVERED";

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

    final PendingIntent deliveredPI = PendingIntent.getBroadcast(getContext(), 0, new Intent(DELIVERED), 0);

    // ---when the SMS has been sent---

    // SEND BroadcastReceiver
    BroadcastReceiver sendSMS = new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    BaseActivity.displayToastyInfo("Send",getContext());
                    if (state == 1 && ServicesActivity.SelectedContactsSMS.size() > ServicesActivity.countSMS+1 || numbers.size() > ServicesActivity.countSMS+1) {
                        state = 0;
                        ChangeNumberMessage();
                        if (numbers.size() > 0) {
                            left.setText("( " + String.valueOf((numbers.size()) - (ServicesActivity.countSMS+1)) + " )");
                            sent.setText("( " + String.valueOf((ServicesActivity.countSMS+1) - ServicesActivity.ListFailMessage.size()) + " )");
                        } else if(ServicesActivity.SelectedContactsSMS.size() > 0) {
                            left.setText("( " + String.valueOf((ServicesActivity.SelectedContactsSMS.size()) - (ServicesActivity.countSMS+1)) + " )");
                            sent.setText("( " + String.valueOf((ServicesActivity.countSMS+1) - ServicesActivity.ListFailMessage.size()) + " )");
                        }
                        ServicesActivity.countSMS++;
                        Log.d("Count","Count SMS : "+ServicesActivity.countSMS);
                        ChangeNumberMessage();
                        btn_send.performClick();
                    }

                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    if (state == 1 && ServicesActivity.SelectedContactsSMS.size() > ServicesActivity.countSMS+1 || numbers.size() > ServicesActivity.countSMS+1) {
                        state = 0;
                        ChangeNumberMessage();
                        // Set Number To Fail SMS List
                        if (numbers.size() > 0) {
                            ServicesActivity.ListFailMessage.add(numbers.get(ServicesActivity.countSMS));
                            left.setText("( " + String.valueOf((numbers.size()) - (ServicesActivity.countSMS+1)) + " )");
                            fail.setText("( " + String.valueOf(ServicesActivity.ListFailMessage.size()) + " )");

                        } else if(ServicesActivity.SelectedContactsSMS.size() > 0) {
                            ServicesActivity.ListFailMessage.add(ServicesActivity.SelectedContactsSMS.get(ServicesActivity.countSMS).getNumber());
                            left.setText("( " + String.valueOf((ServicesActivity.SelectedContactsSMS.size()) - (ServicesActivity.countSMS+1)) + " )");
                            fail.setText("( " + String.valueOf(ServicesActivity.ListFailMessage.size()) + " )");
                        }
                        ServicesActivity.countSMS++;
                        Log.d("Count","Count SMS : "+ServicesActivity.countSMS);
                        ChangeNumberMessage();
                        btn_send.performClick();
                    }

                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    if (state == 1 && ServicesActivity.SelectedContactsSMS.size() > ServicesActivity.countSMS+1 || numbers.size() > ServicesActivity.countSMS+1) {
                        state = 0;
                        ChangeNumberMessage();
                        // Set Number To Fail SMS List
                        if (numbers.size() > 0) {
                            ServicesActivity.ListFailMessage.add(numbers.get(ServicesActivity.countSMS));
                            left.setText("( " + String.valueOf((numbers.size()) - (ServicesActivity.countSMS+1)) + " )");
                            fail.setText("( " + String.valueOf(ServicesActivity.ListFailMessage.size()) + " )");

                        } else if(ServicesActivity.SelectedContactsSMS.size() > 0) {
                            ServicesActivity.ListFailMessage.add(ServicesActivity.SelectedContactsSMS.get(ServicesActivity.countSMS).getNumber());
                            left.setText("( " + String.valueOf((ServicesActivity.SelectedContactsSMS.size()) - (ServicesActivity.countSMS+1)) + " )");
                            fail.setText("( " + String.valueOf(ServicesActivity.ListFailMessage.size()) + " )");
                        }
                        ServicesActivity.countSMS++;
                        Log.d("Count","Count SMS : "+ServicesActivity.countSMS);
                        ChangeNumberMessage();
                        btn_send.performClick();
                    }
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    if (state == 1 && ServicesActivity.SelectedContactsSMS.size() > ServicesActivity.countSMS+1 || numbers.size() > ServicesActivity.countSMS+1) {
                        state = 0;
                        ChangeNumberMessage();
                        // Set Number To Fail SMS List
                        if (numbers.size() > 0) {
                            ServicesActivity.ListFailMessage.add(numbers.get(ServicesActivity.countSMS));
                            left.setText("( " + String.valueOf((numbers.size()) - (ServicesActivity.countSMS+1)) + " )");
                            fail.setText("( " + String.valueOf(ServicesActivity.ListFailMessage.size()) + " )");

                        } else if(ServicesActivity.SelectedContactsSMS.size() > 0) {
                            ServicesActivity.ListFailMessage.add(ServicesActivity.SelectedContactsSMS.get(ServicesActivity.countSMS).getNumber());
                            left.setText("( " + String.valueOf((ServicesActivity.SelectedContactsSMS.size()) - (ServicesActivity.countSMS+1)) + " )");
                            fail.setText("( " + String.valueOf(ServicesActivity.ListFailMessage.size()) + " )");
                        }
                        ServicesActivity.countSMS++;
                        Log.d("Count","Count SMS : "+ServicesActivity.countSMS);
                        ChangeNumberMessage();
                        btn_send.performClick();
                    }

                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    if (state == 1 && ServicesActivity.SelectedContactsSMS.size() > ServicesActivity.countSMS+1 || numbers.size() > ServicesActivity.countSMS+1) {
                        state = 0;
                        ChangeNumberMessage();
                        // Set Number To Fail SMS List
                        if (numbers.size() > 0) {
                            ServicesActivity.ListFailMessage.add(numbers.get(ServicesActivity.countSMS));
                            left.setText("( " + String.valueOf((numbers.size()) - (ServicesActivity.countSMS+1)) + " )");
                            fail.setText("( " + String.valueOf(ServicesActivity.ListFailMessage.size()) + " )");

                        } else if(ServicesActivity.SelectedContactsSMS.size() > 0) {
                            ServicesActivity.ListFailMessage.add(ServicesActivity.SelectedContactsSMS.get(ServicesActivity.countSMS).getNumber());
                            left.setText("( " + String.valueOf((ServicesActivity.SelectedContactsSMS.size()) - (ServicesActivity.countSMS+1)) + " )");
                            fail.setText("( " + String.valueOf(ServicesActivity.ListFailMessage.size()) + " )");
                        }
                        ServicesActivity.countSMS++;
                        Log.d("Count","Count SMS : "+ServicesActivity.countSMS);
                        ChangeNumberMessage();
                        btn_send.performClick();
                    }

                    break;
            }
        }
    };

    // DELIVERY BroadcastReceiver
    BroadcastReceiver deliverSMS = new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    ChangeNumberMessage();
                    if (numbers.size() > 0) {
                        left.setText("( " + String.valueOf((numbers.size()) - (ServicesActivity.countSMS+1)) + " )");
                        sent.setText("( " + String.valueOf((ServicesActivity.countSMS+1) - ServicesActivity.ListFailMessage.size()) + " )");
                    } else if(ServicesActivity.SelectedContactsSMS.size() > 0) {
                        left.setText("( " + String.valueOf((ServicesActivity.SelectedContactsSMS.size()) - (ServicesActivity.countSMS+1)) + " )");
                        sent.setText("( " + String.valueOf((ServicesActivity.countSMS+1) - ServicesActivity.ListFailMessage.size()) + " )");
                    }

                    break;
                case Activity.RESULT_CANCELED:
                    ChangeNumberMessage();
                    // Set Number To Fail SMS List
                    if (numbers.size() > 0) {
                        ServicesActivity.ListFailMessage.add(numbers.get(ServicesActivity.countSMS));
                        left.setText("( " + String.valueOf((numbers.size()) - (ServicesActivity.countSMS+1)) + " )");
                        fail.setText("( " + String.valueOf(ServicesActivity.ListFailMessage.size()) + " )");
                    } else if(ServicesActivity.SelectedContactsSMS.size() > 0) {
                        ServicesActivity.ListFailMessage.add(ServicesActivity.SelectedContactsSMS.get(ServicesActivity.countSMS).getNumber());
                        left.setText("( " + String.valueOf((ServicesActivity.SelectedContactsSMS.size()) - (ServicesActivity.countSMS+1)) + " )");
                        fail.setText("( " + String.valueOf(ServicesActivity.ListFailMessage.size()) + " )");
                    }

                    break;
            }
        }
    };
SmsManager.getDefault().sendTextMessage(phone, null, smsText, sentPI, deliveredPI);

0 个答案:

没有答案