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



我正试图从远程服务器获取数据,试图在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);

更新类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();
}

在您的活动

/.../
new FirstRunDBCreator(baseContext).execute();

相关内容

  • 没有找到相关文章

最新更新