gpt4 book ai didi

Android 将数据传递给广播接收器不起作用

转载 作者:太空狗 更新时间:2023-10-29 14:54:56 25 4
gpt4 key购买 nike

我想发送多条短信。我有一个 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();
}

最佳答案

在 Intent 中设置数据

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);
}

关于Android 将数据传递给广播接收器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32451481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com