Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to correctly use the AlarmManager class to execute a task at fixed intervals?

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 284
    Comment on it

    This Post ll help you to manage your alarm :

    AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
    
    Intent alarmintent1=new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent sender1=PendingIntent.getBroadcast(SpeedMotors.this, 100, alarmintent1, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
    
    try {
        am.cancel(sender1);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("ex....."+e);
    }
    
    Calendar cal=Calendar.getInstance();
    cal.add(Calendar.SECOND, 5);
    
    
    am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000*10, sender1);
    System.out.println("alarm set...........");
    

    We can also set one time alarm by using set(int type, long triggerAtMillis, PendingIntent operation) method.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: