I would suspect this Activity-Service communication pattern is being
implemented in a variety of ways with good reason.  Either the
developer is a newbie and found a way that works (probably from Mark's
book) or else they are strategically searching for a solution that
works specifically for their use case.  I am a newbie looking for the
specific case!

My application needs to have a daemon-like thread running to collect
statistics.  I chose the Service here because it looked like it has a
lesser chance of being killed-- especially when conditioned with the
setForeground(true).  Perhaps I could replace the service with a
Broadcast receiver and store my statistics in an Application static
structure as Mark suggests.  My UI Activity *does* depend on the
collected statistics for presentation and that Activity can be killed
and resurrected any number of times without causing a problem.
However, if the Service is stopped, the application will miss events
that statics are beg compiled over and the application will
effectively become useless!  Does the Android Application lifecycle
support the notion of a daemon thread that will not get booted out of
memory?  I have read about lifecycles and precious resources and my
service is as skinny as they come.


What strikes me as odd is how on the one hand Android touts being able
to run background tasks when all the while you are encouraged not to
do it!

I am currently updating the Activity UI from the Service via a stashed
Handler in the Application scope.  Generally my design is as follows:

AppStatistics extends Application and contains the current Activity
instance and stats
public class AppStatistics extends Application {
    private PlanTrak planTrak;
    // internal statistics here
PlanTrak is my UI
public class PlanTrak extends Activity {

    Handler guiHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        guiHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
          // blah, blah, UI updates
};
        ((AppStatistics) getApplication()).setPlanTrak(this);

    }
}
PlanTrakService is my “daemon”
public class PlanTrakService extends Service {
     AppStatistics app;
    @Override public void onCreate() {
      super.onCreate();
      // I read this is supposed to raise the priority of this Service
to minimize it being killed
      setForeground(true);
    app = (AppStatistics) getApplication();

      // init the service here
      _startService();
    }

    @Override public void onDestroy() {
      super.onDestroy();
      _shutdownService();
    }

    private void _startService() {
      timer.scheduleAtFixedRate(
          new TimerTask() {
            public void run() {
              _monitorStats();
            }
          },
          0,
          UPDATE_INTERVAL);
      Log.i(getClass().getSimpleName(), "Timer started!!!");
    }

    private void _monitorStats() {
      app.blahStatsUpdate();
      Message message = Message.obtain();
      message.arg1=app.getBlahStatsUpdate();
      app.getPlanTrak().getGuiHandler().sendMessage(message);
    }

    private void _shutdownService() {
      if (timer != null) timer.cancel();
    }
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to