即使在使用 AsyncTask 之后,编舞者也会跳帧(高达 400!)
Choreographer skipping frames(upto 400!) even after using AsyncTask
我正在尝试从我的远程服务器获取数据,尝试在 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 之外,我没有任何高分辨率图像或任何繁重的前端组件,而且所有繁重的处理都是在后台线程中执行的,这是什么导致编舞者跳帧?是因为我在 onCreate()
方法中调用 AsyncTask 还是因为我在 onPreExecute()
或 onPostExecute()?
中有一些代码 是 Runnable run()
还是任何其他原因就此而言?
当您调用 .get() 方法时,它将在当前线程上运行,在这种情况下是 UI 线程,因此请尝试在没有 get
.
的情况下执行
推荐asyncTask使用weakReference上下文,像这样:
/.../in your activity
WeakReference<Context> baseContext = new WeakReference<>(MainActivity.this);
已更新 class FirstRunDBCreator
public class FirstRunDBCreator extends AsyncTask<Void, Void, String> {
private WeakReference<Context> context;
public FirstRunDBCreator(WeakReference<Context> context) {
this.context = context;
}
@Override
protected String doInBackground(Void... voids) {
Context baseContext = context.get();
SharedPreferences sp = baseContext.getSharedPreferences("android.content.SharedPreference", Context.MODE_PRIVATE);
if(!sp.contains("firstRun")) {
sp.edit().putBoolean("firstRun", true).apply();
}
String result = "";
//initialize DataBase from baseContext
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
context.clear();
}
在你的activity
/.../
new FirstRunDBCreator(baseContext).execute();
我正在尝试从我的远程服务器获取数据,尝试在 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 之外,我没有任何高分辨率图像或任何繁重的前端组件,而且所有繁重的处理都是在后台线程中执行的,这是什么导致编舞者跳帧?是因为我在 onCreate()
方法中调用 AsyncTask 还是因为我在 onPreExecute()
或 onPostExecute()?
中有一些代码 是 Runnable run()
还是任何其他原因就此而言?
当您调用 .get() 方法时,它将在当前线程上运行,在这种情况下是 UI 线程,因此请尝试在没有 get
.
推荐asyncTask使用weakReference上下文,像这样:
/.../in your activity
WeakReference<Context> baseContext = new WeakReference<>(MainActivity.this);
已更新 class FirstRunDBCreator
public class FirstRunDBCreator extends AsyncTask<Void, Void, String> {
private WeakReference<Context> context;
public FirstRunDBCreator(WeakReference<Context> context) {
this.context = context;
}
@Override
protected String doInBackground(Void... voids) {
Context baseContext = context.get();
SharedPreferences sp = baseContext.getSharedPreferences("android.content.SharedPreference", Context.MODE_PRIVATE);
if(!sp.contains("firstRun")) {
sp.edit().putBoolean("firstRun", true).apply();
}
String result = "";
//initialize DataBase from baseContext
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
context.clear();
}
在你的activity
/.../
new FirstRunDBCreator(baseContext).execute();