Hander Message 传递对象
我们在Message的源码中发现
- public final class Message implements Parcelable {
- /**
- * User-defined message code so that the recipient can identify
- * what this message is about. Each {@link Handler} has its own name-space
- * for message codes, so you do not need to worry about yours conflicting
- * with other handlers.
- */
- public int what;
- /**
- * arg1 and arg2 are lower-cost alternatives to using
- * {@link #setData(Bundle) setData()} if you only need to store a
- * few integer values.
- */
- public int arg1;
- /**
- * arg1 and arg2 are lower-cost alternatives to using
- * {@link #setData(Bundle) setData()} if you only need to store a
- * few integer values.
- */
- public int arg2;
- /**
- * An arbitrary object to send to the recipient. When using
- * {@link Messenger} to send the message across processes this can only
- * be non-null if it contains a Parcelable of a framework class (not one
- * implemented by the application). For other data transfer use
- * {@link #setData}.
- *
- * <p>Note that Parcelable objects here are not supported prior to
- * the {@link android.os.Build.VERSION_CODES#FROYO} release.
- */
- public Object obj;
以上值得注意的地方是:
//可以注意这里的解释,这是一个任意的对象,用来传递给接收者
//对于其他的数据,如基本类型可以使用setData
//注意点是:这里不支持Parcelable类型的对象
于是可以像下面这样来使用
- if(handler!=null){
- Message message = handler.obtainMessage();
- message.what = 123123;
- message.obj=new Object();
- handler.sendMessageDelayed(message, 1000);
- }
转载于:https://blog.51cto.com/zilla/840858