Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think having a service to manage the GPS is the sensible way to go, especially if you have more than one activity which might want location information. I adopted such an approach in my app.</p> <p>Firstly I used an <code>Ibinder</code> in the service and bound to it from the activities with </p> <pre><code>bindService (new Intent(....), mServconn, Context.BIND_AUTO_CREATE); </code></pre> <p>in onStart()</p> <p>and <code>unbindService(mServconn);</code> in onStop() of each activity</p> <p>I had the service use <code>sendBroadcast()</code> to <code>BroadcastReceivers</code> registered in the activities. The location data is passed via extras in the broadcast intent.</p> <p>I used a state machine in the service with 3 states, IDLE, SEEKING and GOT_A_FIX_NOW_SLEEPING. The sleep time is passed in through a 'changeGPSParameters' method exposed in a public method of the service. Another parameter is the required accuracy, i.e. don't broadcast a message until you've had a fix better than the the required accuracy, then you can sleep. Sleep means turn off the GPS until the time has elapsed.</p> <p>The timing is managed by a Runnable and a Handler posts messages to it with code like</p> <pre><code>mHandler.postDelayed(this, mSleepTime ); </code></pre> <p>I find this works well. When no activities are bound to the service then <code>onUnbind()</code> will run in the service. In that method you just have to make sure that you stop the location listener and stop the timer with <code>mHandler.removeCallbacks</code></p> <p>UPDATE</p> <p>Below is a simple example of a Runnable which you can start/stop by means of two buttons in your main.xml which should have a single textview to show the timer state:</p> <pre><code>import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.TextView; public class TimerLoopActivity extends Activity { private Handler mHandler = new Handler(); private int mSleepTime = 2; //seconds private int mLoopCount = 0; private Runnable mUpdateTimeTask = new Runnable() { public void run() { // Code here for when timer completes mLoopCount++; setTextBoxMsg("Running - count = " + mLoopCount); mHandler.removeCallbacks(this); mHandler.postDelayed(this, mSleepTime * 1000); // keep looping } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setTextBoxMsg("Timer Idle"); } private void setTextBoxMsg(String string) { TextView tv = (TextView) findViewById(R.id.textView1); tv.setText(string); } public void myClickHandler(View target) { switch (target.getId()) { case R.id.startbutton: setTextBoxMsg("Starting timer"); startTimer(); break; case R.id.stopbutton: setTextBoxMsg("Stopping timer"); mLoopCount = 0; stopTimer(); break; } } private void stopTimer() { mHandler.removeCallbacks(mUpdateTimeTask); } private void startTimer() { mHandler.postDelayed(mUpdateTimeTask, mSleepTime * 1000);} } </code></pre> <p>You can adapt this to put it in your service.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload