2015-09-08 1 views
3

Я хочу отправить несколько смс. У меня есть цикл while, который читает из базы данных и передает идентификатор сообщения в приемник широковещательной передачи, отмечает сообщение как отправленное. Если я пытаюсь отправить несколько сообщений, широковещательная передача получает только идентификатор для первых сообщений, даже когда отправленное сообщение не является первым. Вы ожидаете, что широковещательный приемник получит идентификатор для соответствующих смс, но это не так. Как я могу это исправить?Android передает данные получателю радиопередачи не работает

while (cursor.moveToNext()) { 

      String _ID = cursor.getString(cursor.getColumnIndex(FeedContract.Entry._ID)); 
      int triesCount = cursor.getInt(cursor.getColumnIndex(FeedContract.Entry.TRIES_COUNT)); 
      triesCount += 1; 

      Intent smsSentIntent = new Intent(Constants.SMS_SENT_BROADCAST_NAME); 
      smsSentIntent.putExtra(FeedContract.Entry._ID, _ID); 
      smsSentIntent.putExtra(FeedContract.Entry.TRIES_COUNT, triesCount); 

      Intent smsDeliveredIntent = new Intent(Constants.SMS_DELIVERED_BROADCAST_NAME); 
      smsDeliveredIntent.putExtra(FeedContract.Entry._ID, _ID); 
      smsDeliveredIntent.putExtra(FeedContract.Entry.TRIES_COUNT, triesCount); 

      ArrayList<PendingIntent> sentPendingIntents = new ArrayList<>(); 
      PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, smsSentIntent, 0); 

      ArrayList<PendingIntent> deliveredPendingIntents = new ArrayList<>(); 
      PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, smsDeliveredIntent, 0); 

      try { 
       SmsManager sms = SmsManager.getDefault(); 
       ArrayList<String> mSMSMessage = sms.divideMessage(cursor.getString(cursor.getColumnIndex(FeedContract.Entry.SMS_BODY))); 
       for (int i = 0; i < mSMSMessage.size(); i++) { 
        sentPendingIntents.add(i, sentPI); 
        deliveredPendingIntents.add(i, deliveredPI); 
       } 

       sms.sendMultipartTextMessage(cursor.getString(cursor.getColumnIndex(FeedContract.Entry.RECIPIENTS)), null, mSMSMessage, sentPendingIntents, deliveredPendingIntents);    

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

      try { 
       Thread.sleep(5000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 

     cursor.close(); 
    } 

И на приемнике вещания у меня есть следующее.

@Override 
    public void onReceive(Context context, Intent intent) { 
     Bundle extras = intent.getExtras(); 
     String _ID = extras.getString(FeedContract.Entry._ID); 
     Toast.makeText(context, "Sent Message ID " + _ID, Toast.LENGTH_SHORT).show(); 
     } 

ответ

0

Набор данных в намерении

Intent intent = new Intent(this, AlarmReceiver.class); 
     intent.putExtra(INTENT_MEDICINE_KEY, medicineName); 
     mPendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), REQUEST_CODE, intent, 0); 

И в OnReceive()

@Override 
    public void onReceive(Context context, Intent intent) { 

     String medName = intent.getStringExtra(ReminderActivity.INTENT_MEDICINE_KEY); 

     NotificationManager notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE); 

     Notification notification = new Notification(R.drawable.ic_launcher_notification, context.getString(R.string.medication_alarm), 
       System.currentTimeMillis()); 
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MedicationActivity.class), 0); 
     notification.setLatestEventInfo(context, context.getString(R.string.app_name), context.getString(R.string.notification_message)+" "+medName, 
       contentIntent); 
     notificationManager.notify(R.string.alarm_tag, notification); 

     // Vibrate the mobile phone 
     Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); 
     vibrator.vibrate(2000); 
    } 
+0

Спасибо, но ваш код получает тот же, если используется в цикле – sammyukavi

Смежные вопросы