我有启动画面,可以在进入主要活动之前加载数据库sqlite。而我的问题,进度条布局运行后加载数据库。如果我们lauch app,app变成空白布局(加载数据库) - >进度条运行 - >主要活动。那么,如何在启动画面中使进度条和加载数据库一起运行?
IntroActivity.java:
public class IntroActivity extends Activity implements LoadingTaskFinishedListener {
private SQLiteDatabase db = null;
private DataKamus datakamus = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
datakamus = new DataKamus(this);
db = datakamus.getWritableDatabase();
datakamus.createTable(db);
datakamus.generateData(db);
setContentView(R.layout.activity_intro);
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
new Loading(progressBar, this).execute("");
}
@Override
public void onTaskFinished() {
completeSplash();
}
private void completeSplash(){
startApp();
finish(); // Don't forget to finish this Splash Activity so the user can't return to it!
}
private void startApp() {
Intent intent = new Intent(IntroActivity.this, MainActivity.class);
startActivity(intent);
}
Loading.java:
public class Loading extends AsyncTask<String, Integer, Integer> {
public interface LoadingTaskFinishedListener {
void onTaskFinished();
}
private final ProgressBar progressBar;
private final LoadingTaskFinishedListener finishedListener;
public Loading(ProgressBar progressBar,
LoadingTaskFinishedListener finishedListener) {
this.progressBar = progressBar;
this.finishedListener = finishedListener;
}
@Override
protected Integer doInBackground(String... params) {
Log.i("Tutorial", "Starting task with url: " + params[0]);
if (resourcesDontAlreadyExist()) {
downloadResources();
}
return 1234;
}
private boolean resourcesDontAlreadyExist() {
return true;
}
private void downloadResources() {
int count = 25;
for (int i = 0; i < count; i++) {
int progress = (int) ((i / (float) count) * 100);
publishProgress(progress);
try {
Thread.sleep(100);
} catch (InterruptedException ignore) {
}
}
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progressBar.setProgress(values[0]);
}
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
finishedListener.onTaskFinished();
}
答案 0 :(得分:0)
您应该将所有数据库加载代码移动到asynctask。
答案 1 :(得分:0)
您的简介活动,加载构造函数更改
public class IntroActivity extends Activity implements LoadingTaskFinishedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
new Loading(progressBar, this, IntroActivity.this).execute("");
}
@Override
public void onTaskFinished() {
completeSplash();
}
private void completeSplash(){
startApp();
finish(); // Don't forget to finish this Splash Activity so the user can't return to it!
}
private void startApp() {
Intent intent = new Intent(IntroActivity.this, MainActivity.class);
startActivity(intent);
}
}
你的异步类,
public class Loading extends AsyncTask<String, Integer, Integer> {
public interface LoadingTaskFinishedListener {
void onTaskFinished();
}
private SQLiteDatabase db = null;
private DataKamus datakamus = null;
private final ProgressBar progressBar;
private final LoadingTaskFinishedListener finishedListener;
public Loading(ProgressBar progressBar,
LoadingTaskFinishedListener finishedListener, Context context) {
this.progressBar = progressBar;
this.finishedListener = finishedListener;
this.datakamus = new DataKamus(context);
}
@Override
protected Integer doInBackground(String... params) {
/*
* database
*/
db = datakamus.getWritableDatabase();
datakamus.createTable(db);
datakamus.generateData(db);
Log.i("Tutorial", "Starting task with url: " + params[0]);
if (resourcesDontAlreadyExist()) {
downloadResources();
}
return 1234;
}
private boolean resourcesDontAlreadyExist() {
return true;
}
private void downloadResources() {
int count = 25;
for (int i = 0; i < count; i++) {
int progress = (int) ((i / (float) count) * 100);
publishProgress(progress);
try {
Thread.sleep(100);
} catch (InterruptedException ignore) {
}
}
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progressBar.setProgress(values[0]);
}
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
finishedListener.onTaskFinished();
}
}
看,如果有效。