标签:undle 邮箱 std 接口 turn span images push image
//延迟两秒跳转
newHandler().postDelayed(newRunnable(){
@Override
publicvoid run(){
Intent intent=newIntent(MainActivity.this,TestActivity.class);
startActivity(intent);
}
},2000);
privateProgressBar mProgressBar;
privateint i =0;
privateHandler mHandler =newHandler(){ // 创建Handle
@Override
publicvoid handleMessage(Message msg){
super.handleMessage(msg);
Log.i("-mHandler->",i+"");
mProgressBar.setProgress(i);
}
};
privateRunnable runnable =newRunnable(){
@Override
publicvoid run(){
Log.i("-Runnable->",i+"");
i +=10;
// 要做的事情,这里再次调用此Runnable对象,以实现每两秒实现一次的定时器操作
mHandler.postDelayed(runnable,2000); // 定时器
mHandler.sendMessageDelayed(Message.obtain(),0);// 发送消息才会触发重写的handleMessage方法
}
};
@Override
protectedvoid onCreate(@NullableBundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_handler);
mProgressBar =(ProgressBar) findViewById(R.id.pb_handler);
mHandler.post(runnable);// 开始执行线程
// runnable.run(); // 也可以用这个来开始线程
}
/**
* 删除的时候停止线程操作
*/
@Override
protectedvoid onDestroy(){
super.onDestroy();
mHandler.removeCallbacks(runnable);
}
}
// 两种获取Message的方法
// Returns a new Message from the global message pool.
mHandler.obtainMessage()和Message.obtain()
// 两种方法其实是一样的,只不过一个是通过Handler获取,一个是通过Message的静态方法获得,查询Handler的源码会发现,obtainMessage的方法构成:
publicfinalMessage obtainMessage()
{
returnMessage.obtain(this);
}
Message的recycleUnchecked()方法
void recycleUnchecked(){
// Mark the message as in use while it remains in the recycled object pool.
// Clear out all other details.
flags = FLAG_IN_USE; //int
what =0; // int
arg1 =0; // int
arg2 =0; // int
obj =null; // Object
replyTo =null; // Messenger 信使,信差
sendingUid =-1;
when =0; // long
target =null; // Handler
callback =null; // Runable
data =null; // Bundle
synchronized(sPoolSync){
if(sPoolSize < MAX_POOL_SIZE){
next = sPool;
sPool =this;
sPoolSize++;
}
}
}
mProgressBar.setProgress(msg.arg1);
privateRunnable runnable =newRunnable(){
@Override
publicvoid run(){
Log.i("-Runnable->", i +"");
i +=3;
// 要做的事情,这里再次调用此Runnable对象,以实现每两秒实现一次的定时器操作
mHandler.postDelayed(runnable,300);
Message msg = mHandler.obtainMessage();
msg.arg1 +=i;
mHandler.sendMessage(msg);// 发送消息才会触发重写的handleMessage方法
// mHandler.sendMessageDelayed(Message.obtain(),0); // 发送消息才会触发重写的handleMessage方法
}
};
privateHandler mHandler =newHandler(){
@Override
publicvoid handleMessage(Message msg){
super.handleMessage(msg);
//Log.i("-mHandler->", i + "");
if(msg.arg1 >100){
mHandler.removeCallbacks(runnable);
}else{
mProgressBar.setProgress(msg.arg1);
}
}
};
标签:undle 邮箱 std 接口 turn span images push image
原文地址:http://www.cnblogs.com/his365/p/6060687.html