当我的应用首页加载时,我有一些代码(包括在后面)中运行。 它使用buildconfig和共享首选项来测试它是否是新安装。如果是新安装,则它将执行一系列步骤,这些步骤将有效地使用凌空请求从远程服务器获取数据并将其复制到设备上的sqlite数据库中。
问题:
在这些表之间进行复制的过程需要很长时间,并且由于它们是齐射请求,因此它们在后台运行。因此,这意味着活动完全加载后,齐射请求仍在忙碌运行。然后,我冒着用户与活动交互的风险-可能在将数据复制到sqlite数据库之前打开另一个活动,这可能会导致诸如空值之类的问题。
是否可以在下面的代码中添加进度条或类似的东西,从而在sqlite数据加载完全完成之前禁用用户活动?
完整方法(此方法从活动的oncreate调用)。
private void checkFirstRun(FirebaseUser user){
final String PREF_VERSION_CODE_KEY = "version_code";
final int DOESNT_EXIST = -1;
// Get current version code
int currentVersionCode = BuildConfig.VERSION_CODE;
// Get saved version code
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
int savedVersionCode = prefs.getInt(PREF_VERSION_CODE_KEY, DOESNT_EXIST);
if (currentVersionCode == savedVersionCode) { // This is just a normal run
// check version table
// if changed then reload
// pass intent to home page to proceed as normal
Log.d(TAG, "jjjj1: " + "normal run");
} else if (savedVersionCode == DOESNT_EXIST) { // This is a new install (or the user cleared the shared preferences)
// if user doesnt exist in db add to server db
String strUser = user.getUid();
String strDisplayName = user.getDisplayName();
String strEmail = user.getEmail();
clsServerClass.addUserToServer(strUser, strDisplayName, strEmail, context);
// load tables into sqlite database
clsServerClass.copyTblVersions(context);
clsServerClass.copyAnimalClassTable(context);
clsServerClass.copyAnimalGroupTable(context);
clsServerClass.copyAnimalTable(context);
clsServerClass.copyTblSeasons(context);
clsServerClass.copyRegions(context);
clsServerClass.copyCountries(context);
clsServerClass.copyProvinces(context);
clsServerClass.copyHotspots(context);
clsServerClass.copyHabitats(context);
clsServerClass.getMyPlaces(strUser, context);
clsServerClass.getSightingsUser(strUser, context);
} else if (currentVersionCode > savedVersionCode) { // This is an upgrade
Log.d(TAG, "jjjj1: " + "upgrade");
}
// Update the shared preferences with the current version code
prefs.edit().putInt(PREF_VERSION_CODE_KEY, currentVersionCode).apply();
}
在上面的代码中,这些是对截击请求的调用(如果发生,则在其他情况下发生):
// load tables into sqlite database
clsServerClass.copyTblVersions(context);
clsServerClass.copyAnimalClassTable(context);
clsServerClass.copyAnimalGroupTable(context);
clsServerClass.copyAnimalTable(context);
clsServerClass.copyTblSeasons(context);
clsServerClass.copyRegions(context);
clsServerClass.copyCountries(context);
clsServerClass.copyProvinces(context);
clsServerClass.copyHotspots(context);
clsServerClass.copyHabitats(context);
clsServerClass.getMyPlaces(strUser, context);
clsServerClass.getSightingsUser(strUser, context);
下面是上面的一个clsServerClass调用示例:
public void copyCountries(Context context) {
// drop table first
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS tbl_lookup_countries");
// create table
sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS tbl_lookup_countries(" +
"country_unique_key INT, " +
"country_key INT, " +
"country_name VARCHAR," +
"country_continent_key INT," +
"country_polygon VARCHAR);");
// pull the table from the database
String strUrlReg = getUrl("selectjson", "Select * from tbl_lookup_countries");
final ClsVolleyService clsVolleyServiceReg = new ClsVolleyService(context);
clsVolleyServiceReg.getJsonArraydata(strUrlReg, new ClsVolleyService.VolleyCallback() {
@Override
public void getResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject jsonObject = response.getJSONObject(i);
String str1 = jsonObject.getString("country_unique_key");
String str2 = jsonObject.getString("country_key");
String str3 = jsonObject.getString("country_name");
String str4 = jsonObject.getString("country_continent_key");
String str5 = jsonObject.getString("country_polygon");
String strQuery = "INSERT INTO tbl_lookup_countries (" +
"country_unique_key, " +
"country_key, " +
"country_name, " +
"country_continent_key, " +
"country_polygon) " +
"VALUES(?, ?, ?, ?, ?);";
SQLiteStatement stmt = sqLiteDatabase.compileStatement(strQuery);
stmt.bindString(1, str1);
stmt.bindString(2, str2);
stmt.bindString(3, str3);
stmt.bindString(4, str4);
stmt.bindString(5, str5);
stmt.execute();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
以及从clsServerClass调用的凌空服务请求
public void getJsonArraydata(String url, final VolleyCallback callback){
try {
JsonArrayRequest jsonArrayReq = new JsonArrayRequest
(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, "vvvvq: " + response);
callback.getResponse(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "vvvvq: " + error);
}
});
// Access the RequestQueue through your singleton class.
ClsMySingleton.getInstance(mContext).addToRequestQueue(jsonArrayReq);
} catch(Exception e){
}
}