使用 asynctask 加快 android 应用程序启动时间 [英] using asynctask to speed up android app launch time

查看:16
本文介绍了使用 asynctask 加快 android 应用程序启动时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它从两个网络加载广告,并在启动时将 Flash 文件设置为 webview.这导致启动速度太慢,论坛告诉我使用 asynctask.有人可以将此代码设为 asynctask.

I have an app which load ads from two networks and sets a flash file to webview when started.This is making it too slow on startup, forums told me to use asynctask.Can some one make this code an asynctask.

        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

I can't just make your code an AsyncTask but I can give you an example and some help. This is an example of AsyncTask

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

}

您将在 doInBackground() 中放入的所有网络内容,然后如果您需要更新 UI,您可以在其他方法中完成.完成网络工作后,您可以更新 onPostExecute() 中的 UI.

All of your network stuff you will put in doInBackground() then if you need to update the UI you did that in the other methods. After finishing the network stuff you can update UI in onPostExecute().

这就是你调用任务的方式

This is how you would call the task

TalkToServer myAsync = new TalkToServer() //can add params if you have a constructor
myAsync.execute() //can pass params here for `doInBackground()` method

如果它是您的MainActivity 的内部类,那么它将可以访问MainActivity 的成员变量.如果它是一个单独的类,那么您可以将 context 传递给构造函数,如

If it is an inner class of your MainActivity then it will have access to member variables of MainActivity. If its a separate class then you can pass context to constructor like

TalkToServer myAsync = new TalkToServer(this);

并创建一个构造函数来接受 Context 和你想要的任何其他参数

and create a constructor to accept Context and any other params you want

我强烈建议您阅读以下文档,并确保您了解其工作原理.也许在开始时要了解的最重要的事情是 doInBackground() 不在 UI 上运行,因此您不想尝试更新任何 Views 此处但在其他 AsyncTask 方法中或通过将数据传回 MainActivity 并在那里更新AsyncTask

I strongly suggest going through the docs below and make sure you understand how it works. Maybe the biggest thing to understand when getting started is that doInBackground() doesn't run on the UI so you don't want to try and update any Views here but in the other AsyncTask methods or by passing data back to the MainActivity and update there AsyncTask

这篇关于使用 asynctask 加快 android 应用程序启动时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆