android线程
2015-08-05 23:17
435 查看
与ui相关的代码 都是运行在主线程Main Thread中
其他的线程Worker Thread
原则上:
在安卓系统中 ui线程之外(Worker Thread)是不能修改ui的
特例:ProgressBar progressBar的成员方法 等等
progressBar.setProgress();
在一个应用程序中 主线程通常用于接受用户的输入 以及将 运算的结果 反馈给用户 所以主线程不能进入阻塞的状态 不然无法交互
ANR:application not responding
对于一些可能造成阻塞的操作 必须放在Worker Thread中执行
上述代码能执行
如果将控件改为TextView对象 在MyThread myThread中调用setText()修改文本 最后程序无法运行
其他的线程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()修改文本 最后程序无法运行
相关文章推荐
- Android中<uses-sdk>属性和target属性分析
- Android Api Demos登顶之路(二十二)SetWallpaper
- android 项目地址总结
- Android MotionEvent中getX()和getRawX()的区别
- Android环境搭建的步骤
- Cordova与Android之一 Activity跳转
- android前台
- Android 中View类DrawingCache的使用
- EditText afterTextChanged 中setText使输入法切换回字母输入模式问题
- Android内核学习之二------JNI的使用
- CMD 使用emulator.exe启动Android模拟器——emulator -data 镜像文件名称
- Android - Fragment (三)不同Fragment之间的通信
- [经典技巧]android 如何监听输入法是否弹出或隐藏,监听手机是否是全屏切换
- Cordova android框架详解
- Android学习遇到的一些问题
- android学习四:xml
- Android学习_01——AutoCompleteTextView控件和MultiAutoCompleteTextView控件的应用
- Android 非空格式验证框架的使用,验证邮箱,非空,2选1等等
- Android json解析--GSON
- android的消息处理机制(图+源码分析)——Looper,Handler,Message