Created
September 23, 2014 20:22
-
-
Save skroged/72d577df3283255bd5bf to your computer and use it in GitHub Desktop.
cleaner locks
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(); | |
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 synchronized 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 (ClicksService.this) { | |
ClicksService.this.notify(); | |
} | |
} | |
}.execute(); | |
try { | |
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