Alarm manager don't work after some time

前端 未结 2 621
闹比i
闹比i 2020-12-20 05:09

This is my code. It works correctly, but after some time (about 1 hour) it doesn\'t work. Whats wrong? Thanks in advance.



        
2条回答
  •  醉酒成梦
    2020-12-20 06:05

    public void scheduleAlarm(View V)
    {
        // time at which alarm will be scheduled here alarm is scheduled at 1 day from current time, 
        // we fetch  the current time in milliseconds and added 1 day time
        // i.e. 24*60*60*1000= 86,400,000   milliseconds in a day       
        Long time = new GregorianCalendar().getTimeInMillis()+24*60*60*1000;
    
        // create an Intent and set the class which will execute when Alarm triggers, here we have
        // given AlarmReciever in the Intent, the onRecieve() method of this class will execute when
    
        Intent intentAlarm = new Intent(this, AlarmReciever.class);
    
        //Get the Alarm Service
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    
        //set the alarm for particular time
        alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this,1,  intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
        Toast.makeText(this, "Alarm Scheduled for Tommrrow", Toast.LENGTH_LONG).show();
    
    }_
    

    AlarmReciever Class

    public class AlarmReciever extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            // TODO Auto-generated method stub                                 
            // Your Code     When Alarm willl trigger                             
        }      
    }
    

提交回复
热议问题