android线程
来源:互联网 发布:mac必备配件 编辑:程序博客网 时间:2024/05/13 01:32
与ui相关的代码 都是运行在主线程Main Thread中
其他的线程Worker Thread
原则上:
在安卓系统中 ui线程之外(Worker Thread)是不能修改ui的
特例:ProgressBar progressBar的成员方法 等等
progressBar.setProgress();
在一个应用程序中 主线程通常用于接受用户的输入 以及将 运算的结果 反馈给用户 所以主线程不能进入阻塞的状态 不然无法交互
ANR:application not responding
对于一些可能造成阻塞的操作 必须放在Worker Thread中执行
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/progressBarId" style ="?android:attr/progressBarStyleHorizontal"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/buttonId" android:layout_below="@+id/progressBarId" android:text="Button" android:gravity="center"/></RelativeLayout>
package practice.labyrinth7x.bupt.edu.thread;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.ProgressBar;public class MainActivity extends AppCompatActivity { private Button button; private ProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.buttonId); progressBar = (ProgressBar)findViewById(R.id.progressBarId); ButtonListener buttonListener = new ButtonListener(); button.setOnClickListener(buttonListener); } class ButtonListener implements View.OnClickListener{ @Override public void onClick(View v) { MyThread myThread = new MyThread(); myThread.start(); } } class MyThread extends Thread{ @Override public void run(){ for(int i =0;i < 100;i++){ try { Thread.sleep(2* 1000); } catch (InterruptedException e) { e.printStackTrace(); } progressBar.setProgress(progressBar.getProgress() + 1); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
上述代码能执行
如果将控件改为TextView对象 在MyThread myThread中调用setText()修改文本 最后程序无法运行
0 0
- android线程之多线程
- Android(线程一) 线程
- android线程
- android线程
- android线程
- android线程
- Android线程
- android 线程
- android线程
- android线程
- android 线程
- android 线程
- android 线程
- Android线程
- Android线程
- android线程
- Android线程
- android线程
- web前端的成长之路(START)
- NSBundle路径问题
- C#模板引擎 RazorEngine3.7的扩展 — 引入外部文件
- 排序算法(二)——选择排序及改进
- Android中<uses-sdk>属性和target属性分析
- android线程
- iOS开发之CocoaAsyncSocket学习
- OpenGL着色器语言4.1.7.2(图片类型)-4.1.8(结构体类型)
- hdu 2586 How far away?
- TCP/IP Socket HTTP
- c++基础代码(一)
- 如此抄袭Apps之OscHub(三)
- [ACdream]哗啦啦村的日常游戏(一)抓个球[概率DP][记忆化搜索]
- Codeforces Round #250 (Div. 1) D. The Child and Sequence(线段树暴力)