Android应用在启动时速度较慢

时间:2017-05-20 06:54:20

标签: android performance delay startup

当应用程序第一次启动时,它很慢。在主屏幕显示之前,大约需要5秒钟盯着白色屏幕。之后,该应用程序运行良好,快速。 我不知道为什么我的应用程序在启动时很慢我尝试清理onCreate()并将大多数代码放在onStart()中 这是我的代码:

public class MainActivity extends AppCompatActivity {
private WebView webView;
private  ImageView image ;
private ProgressBar loading;
private TextView textView;
private AdView mAdView;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    image = (ImageView) findViewById(R.id.image);
    webView = (WebView) findViewById(R.id.webview);
    loading = (ProgressBar) findViewById(R.id.progressBar);
    textView = (TextView) findViewById(R.id.textView);
    mAdView = (AdView) findViewById(R.id.adView);




    AdRequest request = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)        // All emulators
            .addTestDevice("00927A7823BCD302CE6574744C8B07DB")  // An example device ID
            .build();

    mAdView.loadAd(request);




    loading.setMax(100);


    //loading.setVisibility(View.VISIBLE);
}

@Override
protected void onStart() {
    super.onStart();

    if(isNetworkAvailable() == false){

        Toast.makeText(MainActivity.this,"no internet connection",Toast.LENGTH_LONG).show();

    }else {

        webView.setWebViewClient(new HelpClient());

        webView.setWebChromeClient(new WebChromeClient() {

            public void onProgressChanged(WebView view, int progress) {

                loading.setVisibility(View.VISIBLE);

                textView.setText(progress + "%");
                loading.setProgress(progress);

                if (progress == 100) {

                   // ++++++++++++++++++++++++++++++++++++++++

                    //gone the progressBar
                    loading.setVisibility(View.GONE);
                    //gone the first logo
                    image.setVisibility(View.GONE);




                    //show webview
                    webView.setVisibility(View.VISIBLE);
                }
                super.onProgressChanged(view, progress);


            }


        });


        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)

        {
            // chromium, enable hardware acceleration
            webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        } else

        {
            // older android version, disable hardware acceleration
            webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        webView.getSettings().setCacheMode(webSettings.LOAD_CACHE_ELSE_NETWORK);
        webView.getSettings().setAppCacheEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        //webView.getSettings().setLoadWithOverviewMode(true);
        //webView.getSettings().setUseWideViewPort(true);
        //webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);
        webSettings.setSaveFormData(true);
        webSettings.setEnableSmoothTransition(true);







        webView.loadUrl("********");


        loading.setProgress(0);


    }


}




@Override
public void onBackPressed() {
    if (webView.canGoBack()) {
        webView.goBack();
    } else {
        super.onBackPressed();
    }
}

private class HelpClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        loading.setVisibility(View.VISIBLE);
        image.setVisibility(View.VISIBLE);
        return true;
    }
}
private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

}

1 个答案:

答案 0 :(得分:0)

实际上你正在webview中加载一个url。因此,移动应用程序的速度总是很慢。

如果您想快速执行应用程序,那么设计自己的布局并通过JSON处理数据的唯一方法就是。