简体   繁体   中英

How can I set date on Alarmmanger?

I have tried this code to alarm at March first. But It doesn't work. How can I solve this problem?

Content of APP


When we press the button(click1), if it was on March first, Alarmmanger calls intent. so this application prints new Activity.


ERROR


When we press the button(click1), then anytime Alarmmanger calls intent.


HOW CAN I SOLVE IT??

-SORRY FOR MY NOT GOOD ENGLISH.. ㅠㅠ

public class MainActivity extends Activity {

private AlarmManager alarm;

private NotificationManager notification;
Calendar calendar = Calendar.getInstance();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);}
    public void click1(View v) {
    notification = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(System.currentTimeMillis());


    calendar.clear();
    calendar.set(cal.YEAR,3,1);


    setAlarm();
    }



private void setAlarm(){

alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent());


}
private PendingIntent pendingIntent() {
    // TODO Auto-generated method stub
    Intent i = new Intent(getApplicationContext(), MainActivity.class);
    PendingIntent p = PendingIntent.getActivity(this, 0, i, 0);
    return p;

}

You don´t need create another Calendar only to obtain the actual year.
When you ask for a new instance of Calendar it will be initialized with the actual date and time Try this:

Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
calendar.set(year,Calendar.MARCH,1);

One other thing, if you set the alarm to a past time, the alarm always will go off.

tl;dr

LocalDate.of( 2016 , Month.MARCH , 1 ).atStartOfDay( ZoneId.of( "America/Montreal" ) ).toInstant().toEpochMilli()

Avoid old date-time classes

You are using troublesome old legacy date-time classes. Now supplanted by the java.time classes.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate ld = LocalDate.of( 2016 , Month.MARCH , 1 );

You can pass a number instead of a Month enum object, numbered sanely 1-12 for January-December.

Time zone

For an alarm we need a time-of-day. You can get the first moment of the day. This is usually the time 00:00:00 , but not always due to to Daylight Saving Time (DST) or other anomalies. Let java.time determine the time.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

ZonedDateTime

Apply a ZoneId to get a ZonedDateTime .

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ld.atStartOfDay( z ) ;

If your alarm library requires this moment be represented as a count of milliseconds since the epoch of start of 1970 UTC, interrogate for a long integer from an extracted Instant object.

long millisecondsSinceEpoch = zdt.toInstant().toEpochMilli() ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date , .Calendar , & java.text.SimpleDateFormat .

The Joda-Time project, now in maintenance mode , advises migration to java.time.

To learn more, see the Oracle Tutorial . And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use… ).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more.

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