My app is for drivers, who can accept or reject trips (which can be multiple) when assigned from the backend. The backend sends the 'task details', including the scheduled start time. The driver can start the trip before the scheduled start time. I have implemented a feature of playing sound 30 minutes before the scheduled start time of trips to remind the driver about the upcoming trip(s). I have used alarms for each accepted trip for this. For this, I have tried to generate unique random numbers to use as the request codes for the alarms. The alarms seem to work fine. However, I need the feature where the alarm gets cancelled in case the user cancels the trip later on, after initially accepting the trip, so that sound isnt't played when the user has cancelled the trip.
I have fetched the task details from the backend, and I have put the taskId (each trip has a unique taskId) , scheduled start time, and request code (unique random number) in Hashmap whenever the user clicks the button to accept the task(trip). I have put this Hashmap in SharedPreference as the 'value' in it.
I generate a unique random number(to use as request code for alarm) then check if it exists in my Hashmap (I fetch the Hashmap from SharedPreference, as I have stored it there). If the number exists in the Hashmap, I generate a different random number. I have used the following code for that:
private int getRandNum() {
// create instance of Random class
Random rand = new Random();
int requestCode;
// Generate random integers in range 0 to 999
requestCode = rand.nextInt(1000);
HashMap<String, DataObject> map = preferencesHelper.getTaskDetails();
if (map != null) {
if (!map.isEmpty()) {
for (Map.Entry<String, DataObject> stringLongEntry : map.entrySet()) {
DataObject dataObject = stringLongEntry.getValue();
if (requestCode == dataObject.getRequestCode()) {
requestCode = getRandNum();
}
}
}
}
return requestCode;
}
I'm trying to implement the functionality of cancelling the alarm on click of button. What is happening is that a different alarm gets cancelled when I implement the cancel functionality on click of button.
These are the methods to implement cancelling of alarm:
Method to cancel alarm on clicking button:
public void onStartTask(Task task, int position) {
.
.
.
HashMap<String, DataObject> map = preferencesHelper.getTaskDetails();
if (map != null) {
for (Map.Entry<String, DataObject> stringLongEntry : map.entrySet()) {
String taskId = String.valueOf(((Map.Entry) stringLongEntry).getKey());
if (taskId.equalsIgnoreCase(task.getTaskId())) {
DataObject dataObject = stringLongEntry.getValue();
CommonUtils.cancelAlarm(getBaseActivity().getApplicationContext(),
ServiceRestartReceiver.ACTION_ALARM_BEFORE_X_MINUTE,
dataObject.getRequestCode(), task.getTaskId());
}
}
}
.
.
.
}
Method that has cancelling alarm functionality:
public static void cancelAlarm(Context context, String action, int requestCode, String taskId) {
PendingIntent pendingIntent = getAlarmPendingIntent(context, action, requestCode, taskId);
AlarmManager alarmMgr1 = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmMgr1.cancel(pendingIntent);
}
Method to get pending intent:
private static PendingIntent getAlarmPendingIntent(Context context, String action, int requestCode, String taskId) {
Intent intents = new Intent(context, ServiceRestartReceiver.class);
intents.setAction(action);
intents.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
if (taskId != null) {
intents.putExtra("taskId", taskId);
}
return PendingIntent.getBroadcast(context, requestCode,
intents, PendingIntent.FLAG_UPDATE_CURRENT);
}
Please help me fix that. If there's no issue in generating random number, is there some work around for codes in my other methods?
Aucun commentaire:
Enregistrer un commentaire