Я пытаюсь получить данные с моего удаленного сервера, пытаюсь создать базу данных в 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();
}
}
});
}
}
Хотя у меня нет изображений с высоким разрешением или каких-либо сложных компонентов переднего плана, кроме двух TextView и того факта, что вся тяжелая обработка выполняется в фоновом потоке, что вызывает хореограф пропустить кадры? Это потому, что я вызываю AsyncTask в методе onCreate()
или у меня есть какой-то фрагмент кода в onPreExecute()
или onPostExecute()?
Это Runnable run()
Или какая-то другая причина по этому поводу?