简体   繁体   中英

date time pickers wrong values for alarm manager?

I am using AlarmManager to set alarm and i want to save its id in my sqlite db, whenever i set the alarm, it fires immediately, i googled the solution, but did'nt find according to my requirement, may be i am getting wrong date and time from my date time picker widgets,
I am using date and time pickers as:

final DatePicker datePicker = (DatePicker)dialog.findViewById(R.id.datePicker);
final TimePicker timePicker = (TimePicker)dialog.findViewById(R.id.timePicker);

Then a dialoge appears, i change date and time, and then then get it as :

Calendar calendar = Calendar.getInstance();
calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), 
timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 0);
timeSetForAlert = calendar.getTimeInMillis()/1000;

And then i pass this to service handler like,

Intent myIntent = new Intent(thisContext, MyReceiver.class);
myIntent.putExtra("timeSetForAlert", timeSetForAlert);
pendingIntent = PendingIntent.getBroadcast(thisContext, 0, myIntent,0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, timeSetForAlert, pendingIntent);

I am skipping broadcast receiver here which takes intent and start alarm service, while alarm service is as:

public void onStart(Intent intent, int startId)
{
       super.onStart(intent, startId);

   long timeSetForAlert = intent.getLongExtra("timeSetForAlert", 0);
   mManager = (NotificationManager) thisContext.getSystemService(thisContext.NOTIFICATION_SERVICE);

   Intent intent1 = new Intent(thisContext,MainActivity.class);

   Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", timeSetForAlert);

   intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

   PendingIntent pendingNotificationIntent = PendingIntent.getActivity( thisContext,0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
   notification.flags |= Notification.FLAG_AUTO_CANCEL;
   notification.setLatestEventInfo(thisContext, "Notification..", "This is a test message!", pendingNotificationIntent);

   mManager.notify(startId, notification);
   Toast.makeText(thisContext, startId+"", Toast.LENGTH_SHORT).show();
}

Please suggest:
1) - Why alarm fires immediately
2) - where should i save it in my db

try using this

 Calendar calendar = Calendar.getInstance();
 calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), 
 timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 0);
 timeSetForAlert = calendar.getTimeInMillis();//try without dividing with 1000

ya it can be solved like this. accept both the answers.. code: Instead of this

        pendingIntent = PendingIntent.getBroadcast(thisContext, 0, myIntent,0);

        PendingIntent pi = PendingIntent.getBroadcast(
                getApplicationContext(), uniqueValue, intent, 0);

instead of "0" use unique value as second parameter to get multipleBroadcast.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM