您的位置:首页 > 移动开发 > Android开发

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中执行

<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()修改文本 最后程序无法运行
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: