Note that there are some explanatory texts on larger screens.

plurals
  1. POSet a Listener in a Service-based Class
    text
    copied!<p>Hy i have a problem to set the ServiceUpdateUIListener in the service to update the UI. It's wrong to make a new Service object and set there the listener and put it in an intent.</p> <p>Code source is at <a href="http://developerlife.com/tutorials/?p=356" rel="nofollow">http://developerlife.com/tutorials/?p=356</a> there i can't find how the set the listener and start the service right.</p> <p>Calling:</p> <pre><code>TimerService service = new TimerService(); TimerService.setUpdateListener(new ServiceUpdateUIListener() { @Override public void updateUI(String time) { clock.setText(time); } }); Intent i = new Intent(Timer.this,service.class); //service cannot be resolved to a type i.putExtra("ms", ms); startService(i); </code></pre> <p>Service:</p> <pre><code> public class TimerService extends Service{ CountDownTimer timer; Chronometer clock; public static ServiceUpdateUIListener UI_UPDATE_LISTENER; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub int ms = intent.getIntExtra("ms", 0); timer = new CountDownTimer(ms,1000){ @Override public void onTick(long millisUntilFinished) { int seconds = (int) (millisUntilFinished / 1000) % 60 ; int minutes = (int) ((millisUntilFinished / (1000*60)) % 60); int hours = (int) ((millisUntilFinished / (1000*60*60)) % 24); clock.setText( String.format("%02d:%02d:%02d", hours,minutes,seconds)); Log.e("Timer", String.valueOf(millisUntilFinished)); } @Override public void onFinish() { // TODO Auto-generated method stub } }.start(); super.onStart(intent, startId); } public static void setUpdateListener(ServiceUpdateUIListener l) { UI_UPDATE_LISTENER = l; } </code></pre>
 

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