Luk*_*uky 434 android android-ui android-asynctask android-view
从在UI线程中运行代码的角度来看,之间有什么区别:
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
Log.d("UI thread", "I am the UI thread");
}
});
Run Code Online (Sandbox Code Playgroud)
要么
MainActivity.this.myView.post(new Runnable() {
public void run() {
Log.d("UI thread", "I am the UI thread");
}
});
Run Code Online (Sandbox Code Playgroud)
和
private class BackgroundTask extends AsyncTask<String, Void, Bitmap> {
protected void onPostExecute(Bitmap result) {
Log.d("UI thread", "I am the UI thread");
}
}
Run Code Online (Sandbox Code Playgroud)
Com*_*are 281
这些都不是完全一样的,尽管它们都具有相同的净效应.
第一和第二之间的区别是,如果你恰巧是在执行代码时,主应用程序线程,则第一个(runOnUiThread())将执行Runnable立即.第二个(post())总是将Runnable事件放在事件队列的末尾,即使您已经在主应用程序线程上.
第三个,假设您创建并执行一个实例BackgroundTask,将浪费大量时间从线程池中获取一个线程,执行默认的无操作doInBackground(),然后最终执行相当于a的操作post().这是三者中效率最低的.使用AsyncTask如果你确实有工作在后台线程做,不只是为使用onPostExecute().
pom*_*ber 242
我喜欢HPP评论中的那个,它可以在没有任何参数的任何地方使用:
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Log.d("UI thread", "I am the UI thread");
}
});
Run Code Online (Sandbox Code Playgroud)
vas*_*art 58
有第四种方式使用 Handler
new Handler().post(new Runnable() {
@Override
public void run() {
// Code here will run in UI thread
}
});
Run Code Online (Sandbox Code Playgroud)
小智 18
Pomber的答案是可以接受的,但我并不是反复创建新对象的忠实粉丝.最好的解决方案总是那些试图减轻记忆力的解决方案.是的,有自动垃圾收集,但移动设备中的内存保护属于最佳实践范围.下面的代码更新了服务中的TextView.
TextViewUpdater textViewUpdater = new TextViewUpdater();
Handler textViewUpdaterHandler = new Handler(Looper.getMainLooper());
private class TextViewUpdater implements Runnable{
private String txt;
@Override
public void run() {
searchResultTextView.setText(txt);
}
public void setText(String txt){
this.txt = txt;
}
}
Run Code Online (Sandbox Code Playgroud)
它可以在任何地方使用:
textViewUpdater.setText("Hello");
textViewUpdaterHandler.post(textViewUpdater);
Run Code Online (Sandbox Code Playgroud)
小智 8
使用处理程序
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
// Code here will run in UI thread
}
});
Run Code Online (Sandbox Code Playgroud)
从Android P开始,您可以使用getMainExecutor():
getMainExecutor().execute(new Runnable() {
@Override public void run() {
// Code will run on the main thread
}
});
Run Code Online (Sandbox Code Playgroud)
返回一个执行程序,该执行程序将在与此上下文关联的主线程上运行排队的任务。这是用于将调用分派到应用程序组件(活动,服务等)的线程。
您可以在Context上调用getMainExecutor()以获取将在主应用程序线程上执行其作业的Executor。还有其他方法可以使用Looper和自定义的Executor实现来实现,但这很简单。
如果需要在Fragment中使用,则应使用
private Context context;
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
((MainActivity)context).runOnUiThread(new Runnable() {
public void run() {
Log.d("UI thread", "I am the UI thread");
}
});
Run Code Online (Sandbox Code Playgroud)
代替
getActivity().runOnUiThread(new Runnable() {
public void run() {
Log.d("UI thread", "I am the UI thread");
}
});
Run Code Online (Sandbox Code Playgroud)
因为在某些情况下会出现空指针异常,例如寻呼机片段
| 归档时间: |
|
| 查看次数: |
246962 次 |
| 最近记录: |