java Android Activity 显示内容的时间过长
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14798927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Activity takes too long to show content
提问by Lisa Anne
My Activity in onCreate()
performs long computations that take some time.
我的 Activity inonCreate()
执行需要一些时间的长时间计算。
In the same onCreate()
I call setContentView()
to set the appearance of the activity.
同样onCreate()
我调用setContentView()
来设置活动的外观。
The point is that, since it takes a while to performs the above mentioned computations the screen of the Activity loads only after long time.
关键是,由于执行上述计算需要一段时间,因此 Activity 的屏幕仅在很长时间后才加载。
Please, any suggestion on how to avoid this?
请问,有什么建议可以避免这种情况吗?
I have tried to call setContentView()
in onCreate()
and start the computations in onResume()
, but again the Activity screen is loaded only at the end.
我曾试着打电话给setContentView()
在onCreate()
开始在计算中onResume()
,但同样的活动画面仅在最后加载。
回答by class stacker
There is no other way than to use e.g. an AsyncTask. The reason is that the actual rendering does not take place asynchronously; in other words, setContentView will only set some data but nothing will be displayed at that point in time.
除了使用例如AsyncTask之外别无他法。原因是实际渲染不是异步发生的;换句话说, setContentView 只会设置一些数据,但在那个时间点不会显示任何内容。
AsyncTask, however, is not necessarily meant for "long" computations. But if your app relies on the result, and no other computations take place in parallel, it may still be the simplest way for you to achieve what you want. If not, you may have to use a Thread even.
然而,AsyncTask 不一定适用于“长”计算。但是,如果您的应用程序依赖于结果,并且没有其他计算并行进行,那么它可能仍然是您实现所需的最简单方法。如果没有,您甚至可能不得不使用线程。
UpdateSince everybody keeps bombarding the original poster with more use AsyncTaskanswers of various quality, I'd like to stress one more time that AsyncTask is intended for shortoperations (to quote the reference: a few seconds at the most) while the OP has given noindication on how long his computations really take. Also, an AsyncTask is a one-shot-only object which can only run once.
更新由于每个人都在用更多不同质量的AsyncTask答案轰炸原始海报,我想再强调一次 AsyncTask 旨在用于短期操作(引用参考文献:最多几秒钟),而 OP 有鉴于没有对他的计算真的多久指示。此外,AsyncTask 是只能运行一次的一次性对象。
One more very important point to consider is the following. Android assigns AsyncTask a background task priority. This means that, besides the lower scheduling priority, the computations in AsyncTask will take ten times as long as if they were performed in the foreground, because Android runs all tasks which have background priority with an artificial limit of 10% CPU cycles. However, AsyncTasks can be lifted out of this group by raising its priority "just a little bit". For an AsyncTask, it would be done like so:
需要考虑的另一个非常重要的点如下。Android 为 AsyncTask 分配后台任务优先级。这意味着,除了较低的调度优先级之外,AsyncTask 中的计算将花费十倍于在前台执行的时间,因为 Android 运行所有具有后台优先级的任务,人为限制 10% 的 CPU 周期。但是,可以通过“稍微”提高其优先级来将 AsyncTask 排除在该组之外。对于 AsyncTask,它会像这样完成:
public R doInBackground(I... is) {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND +
Process.THREAD_PRIORITY_MORE_FAVORABLE);
...
}
回答by Kels
You have to implement AsyncTask
你必须实现 AsyncTask
public class AsyncTaskActivity extends Activity implements OnClickListener {
Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);.
//because we implement OnClickListener we only have to pass "this" (much easier)
btn.setOnClickListener(this);
}
public void onClick(View view){
//detect the view that was "clicked"
switch(view.getId())
{
case R.id.button1:
new LongOperation().execute("");
break;
}
}
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
for(int i=0;i<5;i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
TextView txt = (TextView) findViewById(R.id.output);
txt.setText("Executed"); // txt.setText(result);
//might want to change "executed" for the returned string passed into onPostExecute() but that is upto you
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
回答by StarsSky
If all computations take long time to execute, your UI is 'locked' and not updated.
如果所有计算都需要很长时间才能执行,则您的 UI 将被“锁定”且未更新。
You need to do all long work in an AsyncTask
您需要在AsyncTask 中完成所有长时间的工作
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
AsyncTask 支持正确且轻松地使用 UI 线程。此类允许在 UI 线程上执行后台操作并发布结果,而无需操作线程和/或处理程序。
回答by AAnkit
call setContentView(layoutID) in onCreate method before you start initializing views, create a AsyncTaskand start AsyncTaskthread after you called setContentView in onCreate method only. Something like given below
在开始初始化视图之前,在 onCreate 方法中调用 setContentView(layoutID),仅在 onCreate 方法中调用 setContentView 之后创建一个AsyncTask并启动AsyncTask线程。像下面给出的东西
onCreate(....){
--
--
setContentView(layoutID);
---
--
new asynchTask(); // load your ui in AsyncTask by creating an inner class in your activity by extending AsyncTask class
}
here is a tutorial of how to implement AsyncTask
这是一个关于如何实现 AsyncTask的教程