1

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:

  1. Create an activity with the ui for the settings. Where do you usually store settings? Locale on phone or are there some cloud solutions?
  2. Create an service. What kind of service I need?
  3. How can I trigger the service on startup?
  4. How can I trigger the service every morning (when I have no startup)? AlarmManager?
  5. How can I trigger the service on new or modified calendar events?
  6. 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.

bluepix
  • 107
  • 1
  • 11

2 Answers2

1

Create an activity with the ui for the settings. Where do you usually store settings? Locale on phone or are there some cloud solutions?

on the phone, use sharedpreference or sqlitedatabase for setting

Create an service. What kind of service I need?

simple service, i recomend use START_STICKY

How can I trigger the service on startup?

use boot receiver

How can I trigger the service every morning (when I have no startup)? AlarmManager?

yes alarm manager is good

How can I trigger the service on new or modified calendar events?

read this answer https://stackoverflow.com/a/6175930/2923194

Can I invoke a popup in a service?

create pop uo activity and call it from service

Community
  • 1
  • 1
Meysam
  • 694
  • 6
  • 20
  • Thanks for your answers. It helped me alot. But now I have some follow up questions. See edit above. – bluepix Aug 05 '14 at 14:29
0

Your Question is way too broad. Just providing some simple directions to start with:

  1. Settings can be stored in SHARED PREFERENCES or SQLITE. Related Term CONTENT PROVIDER

  2. Simple REST webservice would be enough to start with. It would connect to your server, fetch the calendar events as a JSON. Consume the webservice response and Parse the JSON and display/save it in SQLITE for later retrieval.

  3. By startup, I assume you want when the app launches the first time. You can have a flag variable in your sharedpreference and call service based on the flag value.

  4. For checking if first day and calling webservice, You can save the date as a value in shared preference and if date is changed, then it is assumed that its the first time user logs in for the day. Trigger appropriate web service then.

  5. Handle events and call web services when required via HTTPclient.

  6. 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?? Yes! It is very much feasible!

Community
  • 1
  • 1
ngrashia
  • 9,869
  • 5
  • 43
  • 58
  • Thanks for your answers. I will use the shared preferences. No, the service must start at phone startup. Now I have some problems with the invoke of the AlertDialog - see edit above. – bluepix Aug 05 '14 at 14:31