如何为android中的每个选项卡使用线程?
我正在开发一个有四个选项卡的应用程序。在每个选项卡中,我实现了每个单独的功能。我想为每个选项卡实现线程,以使程序更高效。有人能建议在选项卡式活动中实现线程的方法吗?您可以使用java线程类或通过扩展AsyncTask类的doInBackground方法来放置正在运行的函数如何为android中的每个选项卡使用线程?,android,multithreading,Android,Multithreading,我正在开发一个有四个选项卡的应用程序。在每个选项卡中,我实现了每个单独的功能。我想为每个选项卡实现线程,以使程序更高效。有人能建议在选项卡式活动中实现线程的方法吗?您可以使用java线程类或通过扩展AsyncTask类的doInBackground方法来放置正在运行的函数 参考:我不认为让每个选项卡在自己的线程中运行有什么好处。Android中线程的优点是在后台执行耗时的工作,这将防止UI锁定。每个选项卡将要做什么 您可以在每个选项卡式活动中使用线程进行后台工作,但如果您计划更新UI,则必须在“
参考:我不认为让每个选项卡在自己的线程中运行有什么好处。Android中线程的优点是在后台执行耗时的工作,这将防止UI锁定。每个选项卡将要做什么
您可以在每个选项卡式活动中使用线程进行后台工作,但如果您计划更新UI,则必须在“主”或UI线程中完成。在android文档中有一篇关于“您可能想从那里尝试”的精彩文章。我建议不要对每个选项卡都采取一种方法,而是尝试将那些可能阻塞UI线程(如下载或解析)的任务保留在后台 这是一个可能对你有帮助的小剪子
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.adapter = new PersonAdapter(this, R.layout.main_item, new ArrayList<Person>());
setListAdapter(this.adapter);
ListView listView=(ListView)findViewById(R.id.section);
sectionAdapter=new SectionAdapter(this,R.layout.section_item,sections);
listView.setAdapter(sectionAdapter);
//execute the filling section task in background
progressDialog = ProgressDialog.show(MainFeedActivity.this,
"Please wait...", "Retrieving data ...", true);
new SectionTask().execute();
new FeedTask().execute();
}
private class SectionTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... arg0) {
//ProcessInformation Here Background Thread you can do downloading and parsing
}
@Override
public void onPostExecute(Void result){
//Update your UI here [UI Thread]
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.adapter=newPersonalAdapter(this,R.layout.main_项,newArrayList());
setListAdapter(this.adapter);
ListView ListView=(ListView)findViewById(R.id.section);
sectionAdapter=新sectionAdapter(此,R.布局。section_项,sections);
setAdapter(sectionAdapter);
//在后台执行填充段任务
progressDialog=progressDialog.show(MainFeedActivity.this,
“请稍候…”,“正在检索数据…”,为true);
新建SectionTask().execute();
新建FeedTask().execute();
}
私有类SectionTask扩展了AsyncTask{
@凌驾
受保护的Void doInBackground(Void…arg0){
//ProcessInformation这里是后台线程,你可以下载和解析
}
@凌驾
public void onPostExecute(作废结果){
//在此处更新您的UI[UI线程]
}
尽管有危险,但要小心