开发者

Alarm service for given time

I need to prompt alarm according to the time values in DB. I came through lot of examples but confusing me. Could any body help to set a repeating alarm for g开发者_运维技巧iven time like 2011-07-03 02:00:00:000 . and it should repeat for 5mints interval.


I suggest you create a service. The service can read the database and set the alarm using AlarmManager class. You can use the AlarmManager's set() or setRepeating() methods based on your use. http://developer.android.com/reference/android/app/AlarmManager.html#set%28int,%20long,%20android.app.PendingIntent%29

here is a sample of AlarmManager usage.

    long triggerAtTime = SystemClock.elapsedRealtime() + triggerAfterTime;
    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, YourAlarmReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);

    mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);

YourAlarmReceiver class (usually) can be a BroadcastReceiver where your logic goes on what happens when the alarm is triggered.


There is no public API for the Calendar application, though since it is a native UI for a Google Calendar, you could push an event over to the Google Calendar via its GData API.

You can use AlarmManager class

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);  
         Calendar calendar = Calendar.getInstance();      
     calendar.setTimeInMillis(System.currentTimeMillis()); 
          calendar.add(Calendar.SECOND, 10);        
   alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜