使用asynctask加快android应用程序启动时间
我有一个应用程序,可以从两个网络加载广告,并在启动时将flash文件设置为webview。论坛告诉我使用asynctask,这会使启动速度太慢。有人能将此代码设置为asynctask吗使用asynctask加快android应用程序启动时间,android,android-asynctask,Android,Android Asynctask,我有一个应用程序,可以从两个网络加载广告,并在启动时将flash文件设置为webview。论坛告诉我使用asynctask,这会使启动速度太慢。有人能将此代码设置为asynctask吗 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); airpush=
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
airpush=new Airpush(getApplicationContext());
airpush.startPushNotification(false);
airpush.startIconAd();
airpush.startDialogAd();
airpush.startAppWall();
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setBackgroundColor(Color.parseColor("#000000"));
mWebView.loadUrl("file:///android_asset/game.swf");
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
花一点时间理解
AsyncTask
的体系结构,比让别人为你做一个更有用
AsyncTask实际上是一个可以扩展和使用的相当简单的类。AsyncTask以其最简单的形式可以是在后台运行的代码(离开UI线程——这就是导致锁定的原因),但设置为允许一些代码在后台运行,一些代码在前后执行,一些代码在必要时作为进度更新执行
您需要创建自己的类来扩展AsyncTask,如下所示。您的任务将包含三个参数。第一个将被传递到在后台运行的doInBackground
函数中,第二个是可以传递到进度更新函数中的参数类型,第三个是要传递到后台函数完成后在UI线程上运行的onPostExecute
fn中的类型。在下面的简单示例中,我将不包括要传递给执行后函数或进度更新函数的类型,因此它们将是Void类型
private class YourTask extends AsyncTask<byte[], Void, Void> {
protected Long doInBackground(byte[]... data) {
//get the array
byte[] array = data[0];
//do something with it.
HERE IS WHERE YOU RUN YOUR CODE IN THE BACKGROUND THAT IS TAKING TOO LONG ON THE UI THREAD
//return null because this type needs to match the last type for returning to the postexec fn
return null;
}
}
因此,通常您可以将花费很长时间的代码粘贴到doInBackground
函数中,但您必须小心,因为它不在UI线程中,并且某些代码必须在UI线程上运行
我建议您进行一些评测,以查看哪些代码会阻塞您的UI线程,并使用异步任务在后台运行这些代码。您可以通过在Eclipse中使用DDMS并使用方法评测来实现这一点。另一种方法是使用Debug
类并调用Debug.startMethodTracing(“tracefilename”)代码>当您想要启动并Debug.stopMethodTracing()时代码>。你可以。但是,您的代码确实加载了一个url(mWebView.loadUrl
),所以我认为这可能是一个很大的瓶颈
作为附录,如果您想要更深入的异步任务示例,这里有一个I C&Pd:
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
int count=url.length;
长totalSize=0;
for(int i=0;i
上面的例子有一些代码,既可以举例说明在后台任务期间更新UI上的进度,也可以传递一个参数,该参数随后由运行post execute fn的UI线程使用。花一点时间理解异步任务的体系结构将比让人理解更有用只需为你做一个
AsyncTask实际上是一个可以扩展和使用的相当简单的类。AsyncTask以其最简单的形式可以是在后台运行的代码(离开UI线程——这就是导致锁定的原因),但设置为允许一些代码在后台运行,一些代码在前后执行,一些代码在必要时作为进度更新执行
您需要创建自己的类来扩展AsyncTask,如下所示。您的任务将包含三个参数。第一个将被传递到在后台运行的doInBackground
函数中,第二个是可以传递到进度更新函数中的参数类型,第三个是要传递到后台函数完成后在UI线程上运行的onPostExecute
fn中的类型。在下面的简单示例中,我将不包括要传递给执行后函数或进度更新函数的类型,因此它们将是Void类型
private class YourTask extends AsyncTask<byte[], Void, Void> {
protected Long doInBackground(byte[]... data) {
//get the array
byte[] array = data[0];
//do something with it.
HERE IS WHERE YOU RUN YOUR CODE IN THE BACKGROUND THAT IS TAKING TOO LONG ON THE UI THREAD
//return null because this type needs to match the last type for returning to the postexec fn
return null;
}
}
因此,通常您可以将花费很长时间的代码粘贴到doInBackground
函数中,但您必须小心,因为它不在UI线程中,并且某些代码必须在UI线程上运行
我建议您进行一些评测,以查看哪些代码会阻塞您的UI线程,并使用异步任务在后台运行这些代码。您可以通过在Eclipse中使用DDMS并使用方法评测来实现这一点。另一种方法是使用Debug
类并调用Debug.startMethodTracing(“tracefilename”)代码>当您想要启动并Debug.stopMethodTracing()时代码>。你可以。但是,您的代码确实加载了一个url(mWebView.loadUrl
),所以我认为这可能是一个很大的瓶颈
作为附录,如果您想要更深入的异步任务示例,这里有一个I C&Pd:
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
int count=url.length;
长totalSize=0;
for(int i=0;i
上面的例子有两个代码来举例说明在U
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
public class TalkToServer extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
@Override
protected String doInBackground(String... params) {
//do your work here
return something;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// do something with data here-display it or send to mainactivity
TalkToServer myAsync = new TalkToServer() //can add params if you have a constructor
myAsync.execute() //can pass params here for `doInBackground()` method
TalkToServer myAsync = new TalkToServer(this);