android基础(1)
/**以下内容主要来源于android-sdk-windows/docs**/
进程和线程
android的UI是非线程安全的,所以不能在工作线程中访问UI对象,两个简单的原则:
1.不要阻塞UI线程
2.不要在除UI线程外的线程中直接访问UI对象
在工作线程中访问UI对象的方法:
Activity.runOnUiThread(Runnable)View.post(Runnable)[url=http://developer.android.com/reference/android/view/View.html#postDelayed(java.lang.Runnable, long)]View.postDelayed(Runnable, long)[/url]
例子:
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { /** The system calls this to perform work in a worker thread and * delivers it the parameters given to AsyncTask.execute() */ protected Bitmap doInBackground(String... urls) { return loadImageFromNetwork(urls[0]); } /** The system calls this to perform work in the UI thread and delivers * the result from doInBackground() */ protected void onPostExecute(Bitmap result) { mImageView.setImageBitmap(result); } } //在工作线程中调用 new DownloadImageTask().execute("http://example.com/image.png"); 在services和content provider中的方法通常要线程安全