-1

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.

endlesschaos
  • 145
  • 1
  • 6

1 Answers1

1
  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;
        }
    }
    
  2. 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);
    
  3. Start service and prevent sleep

    Related answer AlarmManager not working in sleep mode

Community
  • 1
  • 1
songchenwen
  • 1,362
  • 9
  • 14