Android中的线程
来源:互联网 发布:linux编译php扩展 编辑:程序博客网 时间:2024/06/06 21:52
在UI线程中不允许存在阻断的当有需要耗时操作的时候需要另起一个新的线程。例如简单的一个例子:
在页面出现一个圆圈进度条,他是执行一个耗时操作的任务,在这里对其进行睡眠达到这个效果。则需要一个新的子线程来执行这个操作:
PopupWindow pop; ProgressDialog pd; //进度框的显示 public void pdshow(){ pd = new ProgressDialog(this); pd.setTitle("消息框"); pd.setMessage("请稍后。。。"); pd.setCancelable(true); pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); pd.show(); } Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { //super.handleMessage(msg); //关闭进度条 pd.dismiss(); } }; public void btonclick(View v){ //调用显示进度条的方法 pdshow(); //新建立一个子线程让其在三秒之后把刚才打开的进度条关闭 new Thread(){ @Override public void run() { super.run(); for(int i=0; i<3; i++){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } handler.sendEmptyMessage(0); } }.start(); /*LayoutInflater inflater = LayoutInflater.from(this); View myview = inflater.inflate(R.layout.layout_my_dialog, null); //AlertDialog.Builder builder = new AlertDialog.Builder(this); //builder.setView(myview); pop = new PopupWindow(myview, 600, 500); myview.findViewById(R.id.bt_02).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Toast.makeText(MainActivity2.this, "点击了确定", Toast.LENGTH_SHORT).show(); pop.dismiss(); } }); //pop.showAsDropDown(v); pop.showAtLocation(v, Gravity.CENTER, 0, 0);*/ //dialog = builder.create(); //dialog.show(); //builder.show(); }如果把圆圈的进度条转化为长的进度条时:
PopupWindow pop; ProgressDialog pd; public void pdshow(){ pd = new ProgressDialog(this); pd.setTitle("消息框"); pd.setMessage("请稍后。。。"); pd.setCancelable(true); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setMax(10); //最终需要完成的量 pd.show(); } Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { //super.handleMessage(msg); 需要根据不同的message判断应该怎么处理 if(msg.what == 0){ pd.dismiss(); } else { pd.setProgress(msg.arg1); } } }; public void btonclick(View v){ pdshow(); new Thread(){ @Override public void run() { super.run(); for(int i=1; i<11; i++){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } //不在进行发送空消息 而是发送包含message的消息以进行判断 Message msg = Message.obtain(); msg.arg1 = i; msg.what = 1; handler.sendMessage(msg); } handler.sendEmptyMessage(0); } }.start(); /*LayoutInflater inflater = LayoutInflater.from(this); View myview = inflater.inflate(R.layout.layout_my_dialog, null); //AlertDialog.Builder builder = new AlertDialog.Builder(this); //builder.setView(myview); pop = new PopupWindow(myview, 600, 500); myview.findViewById(R.id.bt_02).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Toast.makeText(MainActivity2.this, "点击了确定", Toast.LENGTH_SHORT).show(); pop.dismiss(); } }); //pop.showAsDropDown(v); pop.showAtLocation(v, Gravity.CENTER, 0, 0);*/ //dialog = builder.create(); //dialog.show(); //builder.show(); }在这顺便说一下 当退出一个程序的时候,需求是需要按两次退出时才退出:
实现代码:
private long mExitTime; @Override public void onBackPressed() { //super.onBackPressed(); /*new AlertDialog.Builder(this) .setIcon(R.drawable.ic_launcher) .setTitle("确认对话框") .setMessage("您真的要退出程序么?") .setNegativeButton("取消", null) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }).show();*/ //在第一次点击退出的时候mExitTime默认是为0的所以一开始两个的差值必然是大于2分钟的 然后取当前按下的时间给mExtiTime进行赋值再进行差值判断 if((System.currentTimeMillis()-mExitTime) > 2000){ Toast.makeText(this, "请再按一次退出", Toast.LENGTH_SHORT).show(); mExitTime = System.currentTimeMillis(); } else { finish(); } }
0 0
- Android中的线程模型
- Android中的线程模型
- Android中的线程模型
- Android中的线程模型
- Android中的线程模型
- Android中的线程
- Android中的线程机制
- Android中的线程模型,
- Android中的线程
- Android中的线程处理
- android中的线程
- Android中的线程处理
- Android中的线程池
- Android中的线程小结
- Android中的线程同步
- android中的线程操作
- Android中的异步线程
- android 中的线程倒计时
- android布局------LinearLayout(线性布局)详解
- ACM_lca
- android开发Enum (枚举)的完美替代 —— @IntDef的使用
- 奇异值分解(SVD) --- 几何意义
- Win8+VS2013编译opencv
- Android中的线程
- android前端定制浏览器开发(干货)
- DBA常用脚本和表
- 第6章 CXF的前端应用
- 理解Scala的函数式风格:从var到val的转变
- 根据二叉树的先序和中序来构建二叉树
- Codeforces Round #332 (Div. 2) B. Spongebob and Joke
- 什么是多态?为什么用多态?有什么好处?多态在什么地方用?
- Android 欢迎界面及首次安装导航界面实例