Last active
August 29, 2015 14:06
-
-
Save skroged/ad3961965f132ed90ea8 to your computer and use it in GitHub Desktop.
serializing tasks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ClicksService extends IntentService { | |
private static final String TAG = ClicksService.class.getSimpleName(); | |
final Object locker = new Object(); | |
public ClicksService(String name) { | |
super(name); | |
} | |
public ClicksService() { | |
super("ClicksService"); | |
} | |
@Override | |
protected void onHandleIntent(Intent intent) { | |
int id = intent.getIntExtra("TEST_INT", -1); | |
Log.d(TAG, "intent handled: " + id); | |
dispatchClicks(id); | |
} | |
private void dispatchClicks(final int id) { | |
Log.d(TAG, "creating thread: " + id); | |
new AsyncTask<Void, Void, Void>() { | |
@Override | |
protected Void doInBackground(Void... voids) { | |
Log.d(TAG, "starting thread: " + id); | |
try { | |
Thread.sleep(3000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(Void aVoid) { | |
Log.d(TAG, "finished thread: " + id); | |
synchronized (locker) { | |
locker.notify(); | |
} | |
} | |
}.execute(); | |
synchronized (locker) { | |
try { | |
locker.wait(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
Log.d(TAG, "intent completed: " + id); | |
} | |
@Override | |
public void onDestroy() { | |
Log.d(TAG, "destroyed"); | |
super.onDestroy(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment