I have no idea on where to start but I have made the GUI for my app and need to make things happen based upon the variables I have stored in a SharedPreferences that are based on the Day of the week and time, I would just like to know some of the best ways I can make things happen in the background based on date and time, thank you.
Asked
Active
Viewed 109 times
-1
-
You might want to look at AlarmManager. – k_g Feb 05 '15 at 02:27
1 Answers
1
To run things in background, you need a
Service
public class MyService extends Service { @Override public int onStartCommand(final Intent intent, final int flags, final int startId) { // parse the intent and run your things return START_NOT_STICKY; } }
To to things automatically on specific time, you need
AlarmManager
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context .ALARM_SERVICE); Intent alarmIntent = new Intent(context, BlockchainService.class); // customize your intent PendingIntent pendingIntent = PendingIntent.getService(context, 0, alarmIntent, 0); alarmManager.set(AlarmManager.RTC_WAKEUP, your_specific_time, pendingIntent);
Start service and prevent sleep
Related answer AlarmManager not working in sleep mode

Community
- 1
- 1

songchenwen
- 1,362
- 9
- 14
-
So would the AlarmManager start the service even when the app that contains the alam manger is not open? – endlesschaos Feb 05 '15 at 20:48
-
One more Question, Would My alarm manager start the service even when the device is locked? – endlesschaos Feb 07 '15 at 21:32
-
@endlesschaos found you a related answer http://stackoverflow.com/questions/10206021/alarmmanager-not-working-in-sleep-mode – songchenwen Feb 08 '15 at 08:36