Problem:- I have two broadcast receiver classes. One is called at midnight and it fetches the scheduled alarm time and the second broadcast receiver is called when current time is matched with the store alarm time. But instead of this, AS THE first broadcast receiver is called, the second is also called after finishing the first broadcast receiver function.
Function to call first broadcast receiver:
private void setTheTimeToUpdateTables(Context context) {
Log.i("Update table function","Yes");
AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent=new Intent(context,UpdateTables.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(context,0,alarmIntent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
Calendar alarmStartTime = Calendar.getInstance();
alarmStartTime.setTimeInMillis(System.currentTimeMillis());
alarmStartTime.set(Calendar.HOUR_OF_DAY, 15);
alarmStartTime.set(Calendar.MINUTE, 58);
alarmStartTime.set(Calendar.SECOND, 0);
System.out.println("Updating table time "+alarmStartTime);
System.out.println("Time in millseconds "+alarmStartTime.getTimeInMillis());
alarmManager.setInexactRepeating(AlarmManager.RTC,alarmStartTime.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
Log.d("Alarm","Set for midnight");
}
//First broadcast receiver class
public class UpdateTables extends BroadcastReceiver {
DbHelper dbHelper;
ArrayList<ListMedicine> reminderInfo;
@Override
public void onReceive(Context context, Intent intent) {
dbHelper=new DbHelper(context);
reminderInfo=dbHelper.fetchDataFromReminderinfo();
for (int i=0;i<reminderInfo.size();i++)
{
ListMedicine listMedicine=reminderInfo.get(i);
Calendar calendar=Calendar.getInstance();
System.out.println("Alarm Hour "+listMedicine.getReminder_hour());
System.out.println("Alarm Minutes "+listMedicine.getReminder_min());
calendar.set(Calendar.HOUR_OF_DAY,listMedicine.getReminder_hour());
calendar.set(Calendar.MINUTE, listMedicine.getReminder_min());
setAlarm(calendar,context,i);
}
Log.i("Service Start", CalculateDaysService.TAG);
context.startService(new Intent(context,CalculateDaysService.class));
//Call all the alarm here only.. for next day alarm, call from here.
Log.i("Done","Yes");
}
private void setAlarm(Calendar calNow, Context context, int request_code) {
Log.i("Inside setAlarm,","Yes");
Intent intent = new Intent(context, AlarmReceiver.class);
Long time=calNow.getTimeInMillis();
System.out.println("Reminder Time is "+calNow.get(Calendar.HOUR_OF_DAY)+" "+calNow.get(Calendar.MINUTE));
Log.i("Target",time.toString());
//final int _id = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, request_code, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
}
}
//Second Broadcast Receiver
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context k1, Intent k2) {
// TODO Auto-generated method stub
Log.i("Alarm Received","Yes");
Calendar calendar=Calendar.getInstance();
System.out.println("Alarm Receiver call is "+ calendar.getTime() );
Intent i=new Intent(k1,AlertDialogActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
k1.startActivity(i);
}
}
Manifest :-
<receiver android:name=".AlarmReceiver" android:enabled="true"
android:process=":remote"/>
<receiver android:name=".UpdateTables"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
As I understand you wanna to launch your receiver at alarmStartTime.getTimeInMillis()
time
Calendar alarmStartTime = Calendar.getInstance();
alarmStartTime.setTimeInMillis(System.currentTimeMillis());
alarmStartTime.set(Calendar.HOUR_OF_DAY, 15);
alarmStartTime.set(Calendar.MINUTE, 58);
alarmStartTime.set(Calendar.SECOND, 0);
System.out.println("Updating table time "+alarmStartTime);
System.out.println("Time in millseconds "+alarmStartTime.getTimeInMillis());
and you think
You store and retrieve this value by:
Calendar calendar=Calendar.getInstance();
BUT THIS IS WRONG
To store the value use this:
SharedPreferences prf = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = prf.edit();
editor.putLong("SOMESTRINGVALUETOSAVE",alarmStartTime.getTimeInMillis());
editor.commit();
to obtain:
SharedPreferences prf = PreferenceManager.getDefaultSharedPreferences(context);
long default_val = System.currentTimeInMillis();// I do not know your default value.
long your_millis = prf.getLong("SOMESTRINGVALUETOSAVE", default_value);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.