public class ThumbnailDownloader<Token> extends HandlerThread {
private static final String TAG = "ThumbnailDownloader";
private static final int MESSAGE_DOWNLOAD=0;
Handler handler;
Map<Token,String> requestMap = Collections.synchronizedMap(new HashMap<Token, String>());
@SuppressLint("HandlerLeak")
@Override
protected void onLooperPrepared()
{
handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
if(msg.what == MESSAGE_DOWNLOAD)
{
@SuppressWarnings("unchecked")
Token token =(Token)msg.obj;
Log.i(TAG,"got to request for url :"+ requestMap.get(token));
handleRequest(token);
}
}
};
}
public void queueThumbnail(Token token ,String url)
{
Log.i(TAG,"Got to url :"+url);
requestMap.put(token,url);
handler
.obtainMessage(MESSAGE_DOWNLOAD,token)
.sendToTarget();
}
private void handleRequest(final Token token)
{....}
public void cleanQueue()
{....}
}