使用AsyncTask的加快Android应用程序的启动时间

使用AsyncTask的加快Android应用程序的启动时间

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

问题描述

我有一个应用程序加载由两个联盟的广告,并设置一个Flash文件时started.This正在就启动它太慢了的WebView,论坛告诉我使用asynctask.Can有一个人让这个code时的AsyncTask

 公共无效的onCreate(包savedInstanceState){

    super.onCreate(savedInstanceState);

    的setContentView(R.layout.activity_main);

    airpush =新Airpush(getApplicationContext());
    airpush.startPushNotification(假);
    airpush.startIconAd();
    airpush.startDialogAd();
    airpush.startAppWall();

    mWebView =(web视图)findViewById(R.id.webview);

    mWebView.getSettings()setJavaScriptEnabled(真)。

    mWebView.getSettings()setPluginsEnabled(真)。

    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

    mWebView.setBackgroundColor(Color.parseColor(#000000));

    mWebView.loadUrl(文件:///android_asset/game.swf);

    AD浏览报AD浏览报=(AD浏览报)this.findViewById(R.id.adView);
    adView.loadAd(新AdRequest());
 

解决方案

我不能让你的$ C C $的的AsyncTask 但我可以给你举个例子而一些帮助。这是的AsyncTask

的例子

 公共类TalkToServer扩展的AsyncTask<字符串,字符串,字符串> {
@覆盖
在preExecute保护无效(){
    super.on preExecute();
}

@覆盖
保护无效onProgressUpdate(字符串...值){
    super.onProgressUpdate(值);

}

@覆盖
保护字符串doInBackground(字符串... PARAMS){
//这里做你的工作
    返回的东西;
}

@覆盖
保护无效onPostExecute(字符串结果){
    super.onPostExecute(结果);
       //做一些数据在这里显示,或发送到mainactivity
 

}

所有网络的东西,你会放在 doInBackground()然后如果你需要更新 UI 您这样做,在其它的方法。在完成网络的东西后,你可以更新 UI onPostExecute()

这是你将如何调用任务

  TalkToServer myAsync =新TalkToServer()//如果你有一个构造函数可以添加PARAMS
myAsync.execute()//可以通过PARAMS这里`doInBackground()`方法
 

如果它是一个内部类的 MainActivity 那么将有机会获得 MainActivity 的成员变量。如果它是一个单独的类,那么你可以通过上下文来构造器像

  TalkToServer myAsync =新TalkToServer(本);
 

和创建一个构造函数接受上下文和你想要的任何其他PARAMS

我强烈建议去通过下面的文档,并确保你明白它是如何工作的。也许最重要的事情要明白什么时候起步的是, doInBackground()不上的 UI运行,所以你不要'T想尝试更新任何浏览在这里,但在其他的AsyncTask 方法,或通过将数据传回 MainActivity 和更新有 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());
解决方案

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

}

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

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);

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

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 thereAsyncTask

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

07-29 20:13