gpt4 book ai didi

java - 如何处理 BroadcastReceiver 中的多个警报

转载 作者:行者123 更新时间:2023-12-01 16:47:35 24 4
gpt4 key购买 nike

我正在使用 AlarmReceiver.java 为 WhatsApp 安排多条消息,并且我像这样调用 AlarmReceiver:


ScheduleMessage scheduleMessage = new ScheduleMessage();
scheduleMessage.setPhoneNumber(phoneNumber);
scheduleMessage.setMessage(main_mes);
scheduleMessage.setEnabled(true);
new BackTaskNotification(scheduleMessage).execute();


Intent intent = new Intent(scheduleMessageActivity, AlarmReceiver.class);
intent.putExtra("id",scheduleMessage.getId());
PendingIntent pendingIntent = PendingIntent.getBroadcast(scheduleMessageActivity, scheduleMessage.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) scheduleMessageActivity.getSystemService(ALARM_SERVICE);
if (alarmManager != null) {

calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.YEAR,mYear);
calendar.set(Calendar.MONTH,mMonth);
calendar.set(Calendar.DAY_OF_MONTH,mDay);
Toast.makeText(scheduleMessageActivity, "The message is being schedule for "+ hour +":"+min, Toast.LENGTH_SHORT).show();
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
scheduleMessageActivity.viewPager.setCurrentItem(1);
} else {
Toast.makeText(scheduleMessageActivity, "Enter details", Toast.LENGTH_SHORT).show();
}

我遇到的问题是在 AlarmReceiver.java 中,我无法区分应该使用哪个警报将消息发送到哪个电话号码。 AlarmReceiver.java 的代码:

public  class AlarmReceiver extends BroadcastReceiver {

private DatabaseHelper db;
private CompositeDisposable disposables = new CompositeDisposable();
String phone;
String message;
@Override
public void onReceive(Context context, Intent intent) {

Intent intent1 = new Intent();
String id = intent1.getStringExtra("id");

db = DatabaseHelper.getDatabase(context);
getScheduledMessages(Integer.parseInt(id));


PackageManager packageManager = context.getPackageManager();
Intent i = new Intent(Intent.ACTION_VIEW);
if (!phone.equals("")) {
try {
String url = "https://api.whatsapp.com/send?phone=" + phone + "&text=" + URLEncoder.encode(message, "UTF-8");
i.setPackage("com.whatsapp");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setData(Uri.parse(url));
if (i.resolveActivity(packageManager) != null) {
context.startActivity(i);
}
} catch (Exception e) {
Log.e("tag", "== = = = =" + e.getMessage());
}
}
}

private void getScheduledMessages(final int id){

disposables.add(db.userDao().getScheduledMessage(id)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<List<ScheduleMessage>>() {
@Override
public void accept(List<ScheduleMessage> scheduleMessages) throws Exception {
ArrayList<ScheduleMessage> items = new ArrayList<>(scheduleMessages);

Log.e("qqqqqqqqqqqq","qqqqqqqqqqqq"+items.size()+"---"+id);
for (int i = 0; i < items.size(); i++) {

final ScheduleMessage scheduleMessage = items.get(i);
phone = scheduleMessage.getPhoneNumber();
Log.e("qqqqqqqqqqqq","qqqqqqqqqqqq"+phone);
message = scheduleMessage.getMessage();
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
}
}));
}
}

我只需要在警报接收器类中有一个唯一的ID,通过它我可以从数据库中获取消息和手机号码并执行我的操作。虽然我已经尝试过,但这不是正确的方法,而且不起作用。

希望我能尽快得到帮助,提前致谢:)

最佳答案

您应该使用 Intent 传递给 onReceive 回调,而不是从新 Intent 获取它

        Intent intent1 = new Intent();
String id = intent1.getStringExtra("id");
        String id = intent.getStringExtra("id");

关于java - 如何处理 BroadcastReceiver 中的多个警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61745527/

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