Вопрос

I'm trying to get a notification to fire at a specific time. It occurs to me that I wouldn't have to use an Alarm Manager since I have the "alCal" variable that sets the time. Would it be possible to convert alCal into a Long and use it instead of System.currentTimeMillis(); as the fireTime variable? If so, how would I convert alCal into a Long? Here's my current code:

            //---use the AlarmManager to trigger an alarm---
        AlarmManager aMan = (AlarmManager) getSystemService(ALARM_SERVICE);

            //---get current date and time---
        Calendar alCal = Calendar.getInstance();
        //---sets the time for the alarm to trigger---                       
        alCal.set(Calendar.HOUR_OF_DAY, 23);            
        alCal.set(Calendar.MINUTE, 41);                
        alCal.set(Calendar.SECOND, 00);

        Intent noteIntent = new Intent(this, Splash.class);
        PendingIntent pendI = PendingIntent.getActivity(this, 0, noteIntent, 0);
        long fireTime = System.currentTimeMillis();
        String noteBody = "notification body";
        String noteTitle = "notification title";
        Notification note = new Notification(R.drawable.noteicon, noteTitle, fireTime);
        note.setLatestEventInfo(this, noteTitle, noteBody, pendI);
        note.defaults = Notification.DEFAULT_SOUND;
        noteman.notify(uniqueID, note);
        //---sets the alarm to trigger--- 
        aMan.set(AlarmManager.RTC_WAKEUP, alCal.getTimeInMillis(), pendI); 
        finish();

ok, looks like I can't convert alCal into a long, so I guess I do have to use an AlarmManager. So given the above code, how do I modify it so that the AlarmManger fires the notification at the specified time? I'm new to Android and Java, so I'm not really seeing the sloution. I would think alCal has to be called somewhere in the new Notification call, but I don't know how to do that. Eclipse says I can't just swap fireTime for alCal, so I don't know what to try next.

Это было полезно?

Решение

It occurs to me that I wouldn't have to use an Alarm Manager

Yes, you would.

since I have the "alCal" variable that sets the time.

That is a java.util.Calendar object. On its own, it will not execute your code (such as displaying a Notification) at a particular time.

Would it be possible to convert alCal into a Long and use it instead of System.currentTimeMillis(); as the fireTime variable? If so, how would I convert alCal into a Long?

Call getTimeInMillis().

Bear in mind that your code is not "Setting a notification to fire at a specific time in Android". It will display a Notification immediately, plus set an alarm to invoke an activity at a certain time in the future.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top