我正在使用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 {
}
}));
}
}
我只需要在Alarm Receiver类中使用唯一的ID,就可以通过它从数据库中获取消息和手机号码并执行我的操作。尽管我已经通过意图进行了尝试,但这不是正确的方法,并且无法正常工作。
希望我很快能得到帮助,在此先感谢:)
您应该使用intent传递给onReceive回调,而不是从新的Intent获得它
Intent intent1 = new Intent();
String id = intent1.getStringExtra("id");
String id = intent.getStringExtra("id");