0

I have an app show my school timetable, now i trying to write a DashClock widget. I need to get current day subject from database on specific time like everyday 12am so i can publish the update to DashClock. I check AlarmManager, but have no idea how to set the time for run automatically on 12am and how it access to database. Any example or tutorial? Please help, thanks in advance.

crossRT
  • 616
  • 4
  • 12
  • 26
  • I'm not sure what type of database you're trying to access, so unfortunately I can't help you there. But I would recommend looking into the [TimerTask](http://developer.android.com/reference/java/util/TimerTask.html) class and using a [Timer](http://developer.android.com/reference/java/util/Timer.html) to execute it. – John Thompson Apr 15 '13 at 03:54
  • Hi, @JohnThompson the timetable is downloaded and store to the app SQLite database, it's available for offline use. So now i want to get the data from the SQLite on specific time. Any solution? Thanks for helping! – crossRT Apr 15 '13 at 04:05
  • use the alarm manager and schedule 12 pm for db operation – srinivas Apr 15 '13 at 04:31

1 Answers1

1

You can simply use AlarmManager. By using AlarmManager schedule your task on 12 am at repeating basis. And when AlarmManager Triggers you can perform the database operations.

There are plenty of examples out there. This SO answer can be a good start. you can also check this project. Followings are jsut some search results samples in google first page.

link1, link2, link3

I think from next time better search for your answer before posting.

Community
  • 1
  • 1
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • Yes, thank you. But how do i use AlarmManager to trigger the database operation in service? The last parameter of AlarmManager.setRepeating() is a PendingIntent, i access the database in current service. All the reference they are sending the PendingIntent to other class. – crossRT Apr 15 '13 at 10:24
  • have you checked all the links I provided yet? – stinepike Apr 15 '13 at 10:26
  • yes, I read them 2 times but no idea yet. Did i miss anything? I don't want to send any intent, or can i use AlarmManager to call a function in the service? – crossRT Apr 15 '13 at 11:00
  • you will be catching the alarm event by using broadcastreceiver. in every link there is a broadcastreceiver implemented. in onReceive method you will do the database tasks – stinepike Apr 15 '13 at 15:32
  • My answer here may also help you communicate with your DashClock extension/service: http://stackoverflow.com/a/15571547/102703 – Roman Nurik Apr 16 '13 at 01:54
  • @StinePike : ya, finally i trigger the database operation in broadcastreceiver. But this operation i write the data direct to the service variable, so my service still can have the data. Thank you very much. – crossRT Apr 16 '13 at 12:31