Reputation: 45
I have created an android app which downloads db records from the web service.I want this download operations to be performed while splash screen appears. I have already created a splash screen and written downloading code inside it but my problem is splash screen is not displaying till the end of download. Any help will be appreciated.
For a reference i have included the splash screen activity class below.
public class SplashScreen extends AppCompatActivity{
private static JSONObject jsonResponse;
private static List<customerList> customerLists = new ArrayList<>();
private static List<productList> productLists = new ArrayList<>();
private static List<loginHistoryList> loginHistoryLists = new ArrayList<>();
private static List<salesOrderList> salesOrderLists = new ArrayList<>();
private static List<userList> userLists = new ArrayList<>();
public static Context mContext;
private String TAG = getClass().getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mContext = this;
if(ConnectivityChecker.CheckConnection(mContext)) {
ServerDataFetch mServerDataFetch = new ServerDataFetch();
mServerDataFetch.execute();
}else{
Log.e(TAG,"There is no internet connection");
}
}
/**
* Created by abhilash on 4/3/2016.
*/
private class ServerDataFetch extends AsyncTask<Void,Void,Void> {
UpdateLocalFeeds mUpdateLocalFeeds ;
public String TAG = getClass().getSimpleName();
@Override
protected Void doInBackground(Void... params) {
try {
mUpdateLocalFeeds = new UpdateLocalFeeds(SplashScreen.mContext);
Log.i(TAG, "Streaming data from network: ");
downloadUrl(new VolleyCallback() {
@Override
public void onSuccessResponse(JSONObject result) {
try {
if (result != null) {
Log.d(TAG, result.get("customerList").toString());
Log.i(TAG, "Parsing stream as Atom feed");
Type listType = new TypeToken<Collection<customerList>>() {
}.getType();
customerLists = new GsonBuilder().create().fromJson(result.get("customerList").toString(), listType);
Log.i(TAG, "Customers found " + customerLists.size());
listType = new TypeToken<Collection<loginHistoryList>>() {
}.getType();
loginHistoryLists = new GsonBuilder().create().fromJson(result.get("loginHistoryList").toString(), listType);
Log.i(TAG, "Login History found " + loginHistoryLists.size());
listType = new TypeToken<Collection<productList>>() {
}.getType();
productLists = new GsonBuilder().create().fromJson(result.get("productList").toString(), listType);
Log.i(TAG, "Product found " + productLists.size());
listType = new TypeToken<Collection<salesOrderList>>() {
}.getType();
salesOrderLists = new GsonBuilder().create().fromJson(result.get("salesOrderList").toString(), listType);
Log.i(TAG, "Sales found " + productLists.size());
listType = new TypeToken<Collection<userList>>() {
}.getType();
userLists = new GsonBuilder().create().fromJson(result.get("userList").toString(), listType);
Log.i(TAG, "Users found " + userLists.size());
mUpdateLocalFeeds.updateLocalUserData(userLists);
mUpdateLocalFeeds.updateLocalCustomerData(customerLists);
mUpdateLocalFeeds.updateLocalProductData(productLists);
mUpdateLocalFeeds.updateLocalSalesData(salesOrderLists);
mUpdateLocalFeeds.updateLocalLoginData(loginHistoryLists);
}
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
}
});
}catch (Exception e){
Log.e(TAG,e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Log.d(TAG,"Downloading completed");
Intent intent = new Intent(SplashScreen.this, LoginActivity.class);
startActivity(intent);
finish();
}
private void downloadUrl(final VolleyCallback volleyCallback) throws IOException {
final JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, Constants.SERVER_DATA, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "Response: " + response.toString());
jsonResponse = response;
volleyCallback.onSuccessResponse(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null) {
// HTTP Status Code: 401 Unauthorized
Log.e("SyncAdapter", networkResponse.statusCode+"");
Log.e("SyncAdapter", error.getMessage());
}
}
});
AppController.getInstance().addToRequestQueue(jsObjRequest,"JObject");
}
}
interface VolleyCallback{
void onSuccessResponse(JSONObject result);
}
}
Upvotes: 3
Views: 116
Reputation: 349
the do in background here is useless because you are starting another thread when calling downloadUrl(new VolleyCallback()
the intent must be called after the callback of the downloading is called so move it at the end of onSuccessResponse
Async task is used when you are using your own thread doing something you can control such as buffering for exemple
try this code
private static JSONObject jsonResponse;
private static List<customerList> customerLists = new ArrayList<>();
private static List<productList> productLists = new ArrayList<>();
private static List<loginHistoryList> loginHistoryLists = new ArrayList<>();
private static List<salesOrderList> salesOrderLists = new ArrayList<>();
private static List<userList> userLists = new ArrayList<>();
public static Context mContext;
private String TAG = getClass().getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mContext = this;
if(ConnectivityChecker.CheckConnection(mContext)) {
ServerDataFetch mServerDataFetch = new ServerDataFetch();
mServerDataFetch.execute();
}else{
Log.e(TAG,"There is no internet connection");
}
}
private class ServerDataFetch extends AsyncTask<Void,Void,Void> {
UpdateLocalFeeds mUpdateLocalFeeds ;
public String TAG = getClass().getSimpleName();
@Override
protected Void doInBackground(Void... params) {
try {
mUpdateLocalFeeds = new UpdateLocalFeeds(SplashScreen.mContext);
Log.i(TAG, "Streaming data from network: ");
downloadUrl(new VolleyCallback() {
@Override
public void onSuccessResponse(JSONObject result) {
try {
if (result != null) {
Log.d(TAG, result.get("customerList").toString());
Log.i(TAG, "Parsing stream as Atom feed");
Type listType = new TypeToken<Collection<customerList>>() {
}.getType();
customerLists = new GsonBuilder().create().fromJson(result.get("customerList").toString(), listType);
Log.i(TAG, "Customers found " + customerLists.size());
listType = new TypeToken<Collection<loginHistoryList>>() {
}.getType();
loginHistoryLists = new GsonBuilder().create().fromJson(result.get("loginHistoryList").toString(), listType);
Log.i(TAG, "Login History found " + loginHistoryLists.size());
listType = new TypeToken<Collection<productList>>() {
}.getType();
productLists = new GsonBuilder().create().fromJson(result.get("productList").toString(), listType);
Log.i(TAG, "Product found " + productLists.size());
listType = new TypeToken<Collection<salesOrderList>>() {
}.getType();
salesOrderLists = new GsonBuilder().create().fromJson(result.get("salesOrderList").toString(), listType);
Log.i(TAG, "Sales found " + productLists.size());
listType = new TypeToken<Collection<userList>>() {
}.getType();
userLists = new GsonBuilder().create().fromJson(result.get("userList").toString(), listType);
Log.i(TAG, "Users found " + userLists.size());
mUpdateLocalFeeds.updateLocalUserData(userLists);
mUpdateLocalFeeds.updateLocalCustomerData(customerLists);
mUpdateLocalFeeds.updateLocalProductData(productLists);
mUpdateLocalFeeds.updateLocalSalesData(salesOrderLists);
mUpdateLocalFeeds.updateLocalLoginData(loginHistoryLists);
Log.d(TAG,"Downloading completed");
Intent intent = new Intent(SplashScreen.this, LoginActivity.class);
startActivity(intent);
finish();
}
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
}
});
}catch (Exception e){
Log.e(TAG,e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
private void downloadUrl(final VolleyCallback volleyCallback) throws IOException {
final JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, Constants.SERVER_DATA, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "Response: " + response.toString());
jsonResponse = response;
volleyCallback.onSuccessResponse(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null) {
// HTTP Status Code: 401 Unauthorized
Log.e("SyncAdapter", networkResponse.statusCode+"");
Log.e("SyncAdapter", error.getMessage());
}
}
});
AppController.getInstance().addToRequestQueue(jsObjRequest,"JObject");
}
}
interface VolleyCallback{
void onSuccessResponse(JSONObject result);
}
Upvotes: 0
Reputation: 1591
Just try adding blank onPreExecution:-
@Override
protected void onPreExecute() {
super.onPreExecute();
}
Upvotes: 1
Reputation: 6160
Display the splash screen in the
protected void onPreExecute()
callback and hide it in the
protected void onPostExecute(String result)
Upvotes: 1