- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有以下代码可以使用,但我只是不明白它是如何工作的,首先请引用以下链接
http://developer.android.com/reference/android/os/Message.html#sendToTarget%28%29
它明确指出 sendToTarget() 通过 getTarget() 向指定的 Handler 发送消息,否则它会抛出 NPE。在下面的代码中,我没有看到 getTarget() 的实现,但它工作正常,请解释它是如何工作的,在此处查找 queueThumbnail() 函数..在我不理解它如何工作的地方的注释中指定
public class ThumbnailDownloader<Token> extends HandlerThread {
private static final String TAG = "ThumbNailDownloader";
private static final int MESSAGE_DOWNLOAD = 0;
Handler mHandler;
Map <Token, String> requestMap = Collections.synchronizedMap(new HashMap<Token,String>());
public ThumbnailDownloader(Handler responseHandler)
{
super(TAG);
}
@SuppressLint("HandlerLeak")
@Override
protected void onLooperPrepared()
{
mHandler = new Handler(){
@Override
public void handleMessage(Message msg)
{
if(msg.what==MESSAGE_DOWNLOAD)
{
@SuppressWarnings("unchecked")
Token token = (Token)msg.obj;
Log.i(TAG, "Got a request for Url:"+requestMap.get(token));
handleRequest(token);
}
}
};
}
public void handleRequest(final Token token)
{
try
{
final String url = requestMap.get(token);
if(url == null) return;
byte[] bitmapBytes = new FlickrFetchr().getUrlBytes(url);
final Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length);
Log.i(TAG, "Bitmap Created");
mResponseHandler.post(new Runnable(){
public void run()
{
if(requestMap.get(token)!=url) return;
requestMap.remove(token);
mListener.onThumbnailDownloaded(token, bitmap);
}
});
}
catch(IOException ioe)
{
Log.e(TAG, "Error Downloading Image"+ioe);
}
}
public void queueThumbnail(Token token, String url)
{
Log.i(TAG, "Got an URL: "+url);
requestMap.put(token, url);
mHandler.obtainMessage(MESSAGE_DOWNLOAD, token).sendToTarget();
//some other class calls this function
//here the target is not set, but I see the request is handled well
//how is this possible? getTarget() is not set anywhere is here
}
最佳答案
看看这里https://android.googlesource.com/platform/frameworks/base.git/+/6083d81ce4d67ec632962270fda64ebb9db0d5b1/core/java/android/os/Message.java ,你会知道一切,顺便说一句,如果你正在扩展 HandlerThread,你可能走错了路
关于java - Android Message.sendToTarget() 在没有设置 getTarget() 的情况下工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19087444/
本文整理了Java中org.odata4j.expression.YearMethodCallExpression.getTarget()方法的一些代码示例,展示了YearMethodCallExpr
本文整理了Java中mage.game.events.ZoneChangeEvent.getTarget()方法的一些代码示例,展示了ZoneChangeEvent.getTarget()的具体用法。
我试图建议大型第三方应用程序使用以下切入点/建议来拦截所有字段访问: before(Object target): get(* *) && target(target) && !within(aspe
我正在努力使蓝牙聊天示例与远程服务而不是本地服务一起使用(以供以后使用)。 但是当我尝试使用用于显示发送和接收的消息的处理程序时,会发生错误,我不知道如何修复: mHandler.obtainMess
本文整理了Java中org.jruby.ir.targets.YieldSite.getTarget()方法的一些代码示例,展示了YieldSite.getTarget()的具体用法。这些代码示例主要
我有以下代码可以使用,但我只是不明白它是如何工作的,首先请引用以下链接 http://developer.android.com/reference/android/os/Message.html#s
本文整理了Java中com.android.dx.io.instructions.ZeroRegisterDecodedInstruction.getTarget()方法的一些代码示例,展示了Zero
本文整理了Java中pl.edu.icm.model.bwmeta.y.YRelation.getTarget()方法的一些代码示例,展示了YRelation.getTarget()的具体用法。这些代
方法 getSource()和 getTarget() org.jgrapht.graph.DefaultEdge 上的 DefaultEdge 受到保护。 我应该如何访问 edgeSet() 返回的
我的 WP7 应用程序有问题。我正在尝试编写 WP7 应用程序表单 WPF 示例代码。 private void storyboard_Completed(object sender, Eve
我正在尝试使用INRIA开发的工具spoon来解析一个项目的方法调用,我已经有一个方法mysql表,其中存储了所有方法(methodid、methodname、classid、classname)。我
我是一名优秀的程序员,十分优秀!