简体   繁体   中英

Can't set alarm on with AlarmManager

I've tried many times but nothing.

This is the code of the class which should set the alarm but nothing happens on the specified hour and date.

package com.beppe.reminder;

import java.util.Calendar;
import java.util.Date;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.provider.AlarmClock;

public class ReminderManager {

private Context mCtx;
private AlarmManager alarm;



public ReminderManager(Context Ctx){
    mCtx=Ctx;

}


public void setAlarm(Date d, long taskID){

    alarm=(AlarmManager) mCtx.getSystemService(Context.ALARM_SERVICE);

    Intent intent=new Intent(mCtx, OnAlarm.class);
    intent.putExtra(DBAdapter.KEY_ROWID, taskID);

    PendingIntent pi=PendingIntent.getBroadcast(mCtx, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(System.currentTimeMillis());
    cal.clear();
    cal.set(d.getYear()+1900,d.getMonth(),d.getDate(),d.getHours(),d.getMinutes());


    alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
}

}

The line with d.getYear()+1900 is because the Date class return the year from 1900.

I tried to print date and time and they seems correct (the month is correctly a zero-based int).

If the alarm is correctly set where i could see it?

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);                 

                //---get current date and time---
                Calendar calendar = Calendar.getInstance();       

                //---sets the time for the alarm to trigger---
                calendar.set(Calendar.YEAR, datePicker.getYear());
                calendar.set(Calendar.MONTH, datePicker.getMonth());
                calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());                 
                calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
                calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
                calendar.set(Calendar.SECOND, 0);

                //---PendingIntent to launch activity when the alarm triggers---                    
                Intent i = new Intent("net.learn2develop.DisplayNotification");

                //---assign an ID of 1---
                i.putExtra("NotifID", 1);                                

                PendingIntent displayIntent = PendingIntent.getActivity(
                    getBaseContext(), 0, i, 0);               

                //---sets the alarm to trigger---
                alarmManager.set(AlarmManager.RTC_WAKEUP, 
                    calendar.getTimeInMillis(), displayIntent);
            }

Please refer this link. http://mobiforge.com/developing/story/displaying-status-bar-notifications-android

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