Wup*_*y29 1 performance android
目前,当我开始我的一个活动时,我希望它能够访问几个网页并从那里下载东西(包括大约8000行Json).所有这些代码都花了很长时间.目前代码都是这样的.
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading_mods);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
getActionBar().setDisplayHomeAsUpEnabled(true);
}
// gets ad from google
AdView adView = (AdView) this.findViewById(R.id.ad);
adView.loadAd(new AdRequest());
// prepare for a progress bar dialog
bar = (ProgressBar) (this.findViewById(R.id.progressBar1));
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
bar.setMinimumWidth((width / 5 * 2));
bar.setMax(3);
bar.setVisibility(View.VISIBLE);
TextView text = (TextView) findViewById(R.id.toMods);
text.setText("Checking for updates...");
UpdateChecker update = new UpdateChecker();
update.execute(getBaseContext());
try
{
if (update.get() == true)
{
bar.setMax(5);
text.setText("Downloading update..");
UpdateMods updater = new UpdateMods();
updater.execute(getBaseContext());
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (ExecutionException e)
{
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是,即使(据我所知)在text.setText("正在检查更新......")之后完成Gui需要花费很长时间才能显示出来但是,gui没有t实际显示,直到它在text.setText("Downloading update ..")行或之后.当我在搜索有关如何操作的更多信息时,我发现了这张图片:http: //developer.android.com/images/activity_lifecycle.png
我非常知道我不希望它在onCreate中运行,因为获取gui需要很长时间.onStart或onResume也不可能,因为我不希望每次开始活动时都下载8000+行文件.
那么我应该在哪里运行此代码?(UpdateChecker和UpdateMods是2个ASyncTasks)
你的问题在这里:
if (update.get() == true)
Run Code Online (Sandbox Code Playgroud)
这就是说,实际上,"阻止主应用程序线程并冻结UI直到doInBackground()完成".
摆脱那条线.相反,根据任务的完成情况onPostExecute()对UI进行修改AsyncTask.
| 归档时间: |
|
| 查看次数: |
156 次 |
| 最近记录: |