Search code examples
androidservicebroadcastreceiver

Android trigger broadcast


I dont now if question already exists but i really need help. I need to upload data in intervals (hour, daily etc.). Interval information comes from the spinner (put to SharedPreferences) and when the interval is picked, timer(Handler) needs to start count. The main problem is how to make broadcast receiver to sart on spinner itemchecked, from main activity? Maybe its better way to start it with service, but how to start broadcast on spinner item selected in service. I hope someone understands the problem.


Solution

  • You could write the code that handles the uploads as a service and then start that service via the AlarmManager http://developer.android.com/reference/android/app/AlarmManager.html

    Intent intent = new Intent(this, myUploadService.class);
    PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
    
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, yourWakeUpTime, interval, pintent);
    

    so the intent will be your service.

    yourWakeUpTime is the time when the service should start first.

    interval will be the time selected from your spinner.