Note that there are some explanatory texts on larger screens.

plurals
  1. PORebinding a background service process (Android)
    text
    copied!<p>I have been doing some research on this but found no clear answer. </p> <p>The functional challenge is:</p> <p>a) Start an activity with a layout (OK)</p> <p>b) By Button (labeled START) click ist starts a background service (OK)</p> <p>c) While the activity can be closed, the background service must continue to run (OK)</p> <p>d) The background process is still there, I can see it in my notification (OK)</p> <p>e) The activity restarts from the notification (OK)</p> <p>f) The activity must connect to the process and get the status of it back. Simply checking if the service is running and set the label of the start-stop-button from (b) to STOP. </p> <p>e) Hitting the button will stop the service, so that works.</p> <p>Here are some details:</p> <p>The connection to my service LogoTimerService is done in the activity like this:</p> <pre><code>private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName className, IBinder service) { // We've bound to LocalService, cast the IBinder and get // LocalService instance LogoTimerServiceBinder binder = (LogoTimerServiceBinder) service; mService = binder.getService(); mBound = true; } @Override public void onServiceDisconnected(ComponentName arg0) { mBound = false; } }; </code></pre> <p>ON RESUME I call this function to actually connect:</p> <pre><code>private void connectLogoTimerService() { Log.i(tag, "-connecting service"); Intent intent = new Intent(this, LogoTimerService.class); startService(intent); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); if (mService!=null) Log.i(tag, "-SERVICE BOUND"); else Log.i(tag, "-Service NOT bound"); } </code></pre> <p>When I start the application the first time, I get a the second (NOT bound) message. </p> <p><strong>Why is it not bound? Is it to slow? (It seems like it playing around.)</strong> </p> <p>If I do not destroy the activity the mService is found.</p> <p><strong>How can I synchronize this, so I can get the status back?</strong></p> <p>I start AND bind the service. Is this correct?</p> <p>I can see that the service is running using this code (I found it on this site, thanks):</p> <pre><code>private boolean isMyServiceRunning() { ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); for (RunningServiceInfo service : manager .getRunningServices(Integer.MAX_VALUE)) { if (LogoTimerService.class.getName().equals( service.service.getClassName())) { return true; } } return false; } </code></pre> <p>Thank you very much for your help!</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