Android学习三:线程操作
作为Android开发的组成部分,多线程的作用举足轻重,先来说说应用场景
1多线程使用场景
1.1正常使用中,经常有子线程来更新界面UI的需求,但是安卓不允许子线程更新UI
使用子线程处理UI,若线程需要较长的时间,势必造成UI没有反应,从而导致程序ANR(Application is not responding),使用户体验很差。
1.2多线程执行的需求
与其他编程开发一样,多线程可以增加程序的并发性,提高程序吞吐能力。
2多线程开发模式
2.1使用AsyncTask
AsyncTask是Android框架提供的异步处理的辅助类,含有多个回掉接口,功能相当强大
2.2使用Handler
通过消息队实现线程间的通信,与Looper配合使用,相对于AsyncTask,具有更高的可塑性。
3上代码
public class UIHandler extends Handler{ private EditText mEditText = null;; public UIHandler(EditText editText){ mEditText = editText; } @Override public void handleMessage(Message msg) { super.handleMessage(msg); String value = String.valueOf(msg.obj); if(mEditText!=null){ mEditText.setText(value); } } }
Handler子类的简单实现
public class UIAsyncTask extends AsyncTask<Integer, Void, Integer> { @Override protected void onCancelled() { super.onCancelled(); } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onProgressUpdate(Void... values) { super.onProgressUpdate(values); } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } @Override public boolean equals(Object o) { return super.equals(o); } @Override protected void finalize() throws Throwable { super.finalize(); } @Override public int hashCode() { return super.hashCode(); } @Override public String toString() { return super.toString(); } private EditText mEditText = null; public UIAsyncTask(EditText editText) { mEditText = editText; } @Override protected Integer doInBackground(Integer... param) { return param[0]++; } @Override protected void onPostExecute(Integer result) { mEditText.setText(String.valueOf(result)); } }
AsynTask子类的简单实现
public class MainActivity extends Activity { private EditText mEditText = null; private UIHandler mHandler = null; private UIAsyncTask mAsyncTask = null; private int mCount = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mEditText = (EditText) this.findViewById(R.id.txtTips); mHandler = new UIHandler(mEditText); mAsyncTask = new UIAsyncTask(mEditText); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void OnBtnHandlerClick(View v) { Message msg = mHandler.obtainMessage(); msg.obj = mCount++; mHandler.sendMessage(msg); } public void OnBtnUIThreadClick(View v) { this.runOnUiThread(new Runnable() { public void run() { mEditText.setText(String.valueOf(mCount++)); } }); } public void OnBtnViewPostClick(View v) { //只能被执行一次,否则多次调用时将会出现异常; new Thread(new Runnable() { public void run() { mEditText.setText(String.valueOf(mCount++)); } }); } public void OnBtnAysncClick(View v) { //只能被执行一次,否则多次调用时将会出现异常; mAsyncTask.execute(mCount); } }
MainActivity的内容
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">线程使用</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="info_tips">线程的使用</string> <string name="info_show">等待线程返回信息</string> <string name="handler_method">Handler方法</string> <string name="uithread_method">UIThread方法</string> <string name="uipost_method">ViewPost方法(只能执行一次)</string> <string name="asynctask_method">AsyncTask方法(只能执行一次)</string> </resources>
字符串定义
<LinearLayout 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:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/info_tips" /> <EditText android:id="@+id/txtTips" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/info_show" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="OnBtnHandlerClick" android:text="@string/handler_method" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="OnBtnUIThreadClick" android:text="@string/uithread_method" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="OnBtnViewPostClick" android:text="@string/uipost_method" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="OnBtnAysncClick" android:text="@string/asynctask_method" /> </LinearLayout>
布局文件
4总结
时间比较仓促,写的不是很详细,哎!