Android短信总是发送给同一联系人

时间:2018-10-11 11:30:38

标签: android android-asynctask sms

我正在尝试使用asynctask将短信发送给多个联系人。但是,当我单击“发送”按钮时,短信仅发送到我选择的联系人列表顶部的一个号码。发送的短信数量等于所选联系人列表的大小。我在下面写了短信发送代码。我该如何解决这个问题?

  @Override
  protected final String doInBackground(List<kisiler>... lists) {
      int progress=0;
      final List<contacts> k=lists[0];
      dialog.setMax(k.size());

      while (progress<dialog.getMax()){

            try{

                final String m=k.get(k.size()-1).getName();


                SmsManager smsManager = SmsManager.getDefault();

                publishProgress(String.valueOf(k.size()-1),String.valueOf(k.size()),m,String.valueOf(dialog.getMax()));
                PendingIntent sendPI = PendingIntent.getBroadcast(getActivity(), 0, new Intent(), 0);


                getActivity().registerReceiver(new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        switch (getResultCode()) {
                            case Activity.RESULT_OK:
                          Toast.makeText(getActivity(),getString(R.string.sent_to_sms)+contact.getName(),Toast.LENGTH_LONG).show();

                                break;

                            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                                Toast.makeText(getActivity(), R.string.failure+kisi2.getIsim(),Toast.LENGTH_LONG).show();

                                break;
                            case SmsManager.RESULT_ERROR_NO_SERVICE:

                                Toast.makeText(getActivity(), R.string.no_service, Toast.LENGTH_LONG).show();
                                break;
                            case SmsManager.RESULT_ERROR_NULL_PDU:
                                break;
                            case SmsManager.RESULT_ERROR_RADIO_OFF:
                                break;
                        }
                    }
                }, new IntentFilter(SENT+m));

                smsManager.sendTextMessage(contact.getNumber(), null, location_information(location), sendPI, null);


            }catch (Exception e){
                e.printStackTrace();
            }

          progress++;

      }

      return "Done";
  }

1 个答案:

答案 0 :(得分:1)

您总是引用相同的contact变量。  您需要使用k列表和progress数字更改它,如下所示:

k.get(progress) // your current contact

将其更改为以下内容:

  @Override
  protected final String doInBackground(List<kisiler>... lists) {
      int progress=0;
      final List<contacts> k=lists[0];
      dialog.setMax(k.size());

  while (progress<dialog.getMax()){

        try{

            final String m=k.get(k.size()-1).getName();


            SmsManager smsManager = SmsManager.getDefault();

            publishProgress(String.valueOf(k.size()-1),String.valueOf(k.size()),m,String.valueOf(dialog.getMax()));
            PendingIntent sendPI = PendingIntent.getBroadcast(getActivity(), 0, new Intent(), 0);


            getActivity().registerReceiver(new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    switch (getResultCode()) {
                        case Activity.RESULT_OK:
                      Toast.makeText(getActivity(),getString(R.string.sent_to_sms)+k.get(progress),Toast.LENGTH_LONG).show();

                            break;

                        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                            Toast.makeText(getActivity(), R.string.failure+kisi2.getIsim(),Toast.LENGTH_LONG).show();

                            break;
                        case SmsManager.RESULT_ERROR_NO_SERVICE:

                            Toast.makeText(getActivity(), R.string.no_service, Toast.LENGTH_LONG).show();
                            break;
                        case SmsManager.RESULT_ERROR_NULL_PDU:
                            break;
                        case SmsManager.RESULT_ERROR_RADIO_OFF:
                            break;
                    }
                }
            }, new IntentFilter(SENT+m));

            smsManager.sendTextMessage(k.get(progress).getNumber(), null, location_information(location), sendPI, null);


        }catch (Exception e){
            e.printStackTrace();
        }

      progress++;

  }