Looper之一
来源:互联网 发布:h5页面软件下载 编辑:程序博客网 时间:2024/05/09 09:21
一、Class Overview
Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, callprepare()
in the thread that is to run the loop, and thenloop()
to have it process messages until the loop is stopped.
Most interaction with a message loop is through theHandler
class.
This is a typical example of the implementation of a Looper thread, using the separation ofprepare()
andloop()
to create an initial Handler to communicate with the Looper.
class LooperThread extends Thread { public Handler mHandler; public void run() { Looper.prepare(); mHandler = new Handler() { public void handleMessage(Message msg) { // process incoming messages here } }; Looper.loop(); } }
参照源码:
public static final void prepare() { if (sThreadLocal.get() != null) { throw new RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper()); }
一般情况下只会有主线程会调用prepare方法(ActivityThread的main函数)。
四、开发过程中遇到的情况
btnConfirm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
progressDialog = ProgressDialog.show(
EverydayCheckActivity.this, "提示", "正在上传排查信息");
alertDialog = new AlertDialog.Builder(
EverydayCheckActivity.this)
.setTitle("提示")
.setMessage("排查信息上传成功,是否下发通知单!")
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Intent intent = new Intent();
intent.setClass(
EverydayCheckActivity.this,
NotificationTableActivity.class);
// EverydayCheckActivity.this
// .startActivity(intent);
Toast.makeText(
EverydayCheckActivity.this,
"下发通知单成功", 3000).show();
}
}).setNegativeButton("取消", null).create();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
progressDialog.dismiss();
Looper.prepare();
alertDialog.show();
Looper.loop();
}
}, 3000);
}
});
dismiss不通过消息机制实现,而show通过消息机制实现
- Looper之一
- Android消息机制之一---Looper
- Looper
- Looper
- Looper
- Looper
- Looper
- Looper
- looper
- Looper
- Looper
- 第80章、Handle-Message-Looper消息机制之一(从零开始学Android)
- Android消息处理机制(Handler、Looper、MessageQueue与Message)(面试常问及之一)
- Android部分机制的构架分析之一--Handler和Looper分析
- Android Looper
- Android Looper
- Android Looper
- Android Looper
- Unity3D学习——(二)愤怒的小鸟项目总结
- TCP建立连接与释放连接
- cookie 和session 的区别详解
- cocos2d-x基本元素
- COOKIE的应用
- Looper之一
- 统计键盘输入个数
- 如何将MyEclipse开发的项目导入到Eclipse中运行
- 网页版贪吃蛇游戏开发
- 脚本语言开发平台Script.NET即将开源
- 枚举的基本应用
- 行计数
- Failed to load the JNI shared library
- 实现带有构造方法的枚举