I need your help. I'm a java developer, but not yet a android developer ;)
I wanna develop an application (4.0.3 and above) with these functionalities:
- Application:
+ The Applications starts and offers some settings
- The service:
+ I need a background service which starts on android startup
+ The service has to to trigger and check new calendar events
+ The service checks the events and in some cases it opens a popup
I did some tutorials and checked some android classes. My steps and questions:
- Create an activity with the ui for the settings. Where do you usually store settings? Locale on phone or are there some cloud solutions?
- Create an service. What kind of service I need?
- How can I trigger the service on startup?
- How can I trigger the service every morning (when I have no startup)? AlarmManager?
- How can I trigger the service on new or modified calendar events?
- Can I invoke a popup in a service?
For reading the calendar events I would use the Calendar Provider. For the popup I would create an AlertDialog with a custom layout which is invoked in the service.
Is this feasible? Thanks for your answers and tips.
EDIT
Thanks for your helpful answers. After some tests I have some follow up questions.
The Activity
public class MainActivity extends Activity {
// On the app the user can edit some setting
// But the app needn't be started that the service runs!!
// The service must be able to read the settings from this activity
}
The BroadcastReceiver (for startup)
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
// Start of the service (TriggerService)
}
}
}
The Service for AlarmManager
public class TriggerService extends IntentService {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
@Override
protected void onHandleIntent(Intent intent) {
// force invoke of CalendarChecker if we come from BootReceiver, then
// create the AlarmManager
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, CalendarChecker.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 8:00 a.m.
Calendar calendar = Calendar.getInstance();
...
// Set repeating interval
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}
The calendarChecker:
public class CalendarChecker {
// Checks the calendar events on the device and opens in some case a popup e.g.
...
CustomCalendarDialog dialog = new CustomCalendarDialog();
dialog.show(getFragmentManager(), "showPopup");
}
These is my idea. But now the problems:
- In the TriggerService I need the context of the activity - but how can I get it?
- In the CalendarChecker I need the fragmentmanager of the activity - but how can I get it?
Thanks for your answers.