我想发送多个短信。我有一个while循环,它从数据库中读取并将消息ID传递给广播接收器,将消息标记为已发送。如果我尝试发送多条消息,即使发送的消息不是第一条消息,广播也只接收第一条消息的ID。您可能希望广播接收器接收相应短信的ID,但事实并非如此。我该如何解决这个问题?
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 :(得分: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);
}