When my application installs and is opened for the first time(when there is an internet connection) i want the phone to download some information from my serv开发者_StackOverflow中文版er and insert it into the database.
if the information does not download fully or download at all, this activity should occur untill it fully downloads?
How would i go about doing this?
Use the preferences
in your Activity :
private boolean isFirstLaunch() {
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean isFirstLaunch = settings.getBoolean("isFirstLaunch", true);
Log.i(TAG + ".isFirstLaunch", "sharedPreferences ");
return isFirstLaunch;
}
and from the onCreate
call
if (isFirstLaunch()) {
Intent firstLaunchIntent = new Intent(this,
GetStartedActivity.class);
firstLaunchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(firstLaunchIntent);
// set the bool to false in next activity !
finish();
}
Add a flag in your database, shared preferences or a file that indicates that you've downloaded the data. In onResume, check that flag and whether the device has connectivity. If the flag is false and you have connectivity, attempt to download the data.
If that was successful, update the flag.
Use SharedPreference for this. Create a boolean variable and change it once your operation is complete. In onCreate of your Main/Launcher activity check for this variable and perform operation accordingly. Some psuedo-code.
if (!sharedpreferences.getBoolean("isOpComplete", false)) {
// perform my operation
performOperation();
}
performOperation() {
// Operation complete
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean("isOpComplete", true);
editor.commit();
}
精彩评论