即使在使用AsyncTask之后,编舞者也会在Android中跳过帧(最多400个!)

问题描述 投票:0回答:2

我正在尝试从远程服务器获取数据,尝试在SQLite中创建数据库并更新共享的首选项。我已经将前面提到的所有操作放在AsyncTask中,该操作理论上必须在UI(主)线程之外的单独线程上运行。另外,我正在实际的Android设备上而不是模拟器上运行该应用程序。

我在日志中得到以下信息:

I/Choreographer: Skipped 229 frames!  The application may be doing too much work on its main thread.

源代码:

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {

    TextView errorMessage;
    Button retryAgainButton;

    SharedPreferences sharedPrefs;

    @SuppressLint("StaticFieldLeak")
    public class FirstRunDBCreator extends AsyncTask<Void, Void, String> {
        String result = "";
        private Exception e = null;

        @Override
        protected void onPreExecute() {
            sharedPrefs = getApplicationContext().getSharedPreferences("android.content.SharedPreference", Context.MODE_PRIVATE);
            if(!sharedPrefs.contains("firstRun")) {
                sharedPrefs.edit().putBoolean("firstRun", true).apply();
            }
        }

        @Override
        protected String doInBackground(Void... voids) {
            String result = "";
            // All the heavy operations here
            // Fetching the data from the server here and creating/ storing data in my SQLite database
            // I have also used transactions and SQLite preparedStatement to increase the insert speed
            return result;
        }

        @SuppressLint("SetTextI18n")
        @Override
        protected void onPostExecute(String s) {
            // Closing the database connections and handling any exceptions here
            // Updating the UI elements such as diplaying the complete message or the visibility of a textview
        }
    }

    public void initializeTheUIComponents() {
        errorMessage = findViewById(R.id.errorMessage);
        retryAgainButton = findViewById(R.id.retryAgainButton);
        retryAgainButton.setClickable(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initializeTheUIComponents();
        // Running the jumpToNextActivity after the screen and the UI elements have been rendered
        getWindow().getDecorView().post(new Runnable() {
            @Override
            public void run() {
                try {
                    new FirstRunDBCreator().execute().get();
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

尽管,除了两个TextViews之外,我没有任何高分辨率的图像或任何繁重的前端组件,并且所有繁重的处理都是在后台线程中进行的,这导致了编舞者跳过帧?是因为我正在onCreate()方法中调用AsyncTask,还是因为我在onPreExecute()onPostExecute()?中有一些代码是Runnable run()还是其他原因?

java android android-asynctask
2个回答
0
投票

当您调用.get()方法时,它将在UI线程上运行。尝试在没有它的情况下执行。


0
投票

您正在使用get(),它将阻塞当前线程,直到任务完成以返回响应。

© www.soinside.com 2019 - 2024. All rights reserved.