Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try to use Handler, like here you can use Handler like Thread.</p> <p>here is the example</p> <pre><code>import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; public class MyService extends Service{ private Handler handler; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); handler = new Handler(); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); handler.post(updateStatus); } @Override public void onDestroy() { super.onDestroy(); handler.removeCallbacks(updateStatus); handler = null; } @Override public boolean onUnbind(Intent intent) { handler.removeCallbacks(updateStatus); handler = null; return super.onUnbind(intent); } private Runnable updateStatus = new Runnable() { @Override public void run() { // do something here handler.postDelayed(updateStatus, 1000); } }; } </code></pre> <p>here the handler can was initialized into the oncreate method now after that when onStart method invoked then the updateStatus object will invoked through the handler.post() which will start the run method of this Runnable object.</p> <p>Now in this, this will invoked the run() at once and execute the statement at once only so for repeating this task call inside this method on specific delay time like here 1000 milliseconds so after complete all the execution it will again call after 1 sec and repeating this task this will continue until you cannot remove the runnable object from the handler, so basically that was call into the onDestroy()</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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