网络下载时创建android启动画面

时间:2016-04-05 14:56:10

标签: android database sqlite android-asynctask callback

我创建了一个从Web服务下载数据库记录的Android应用程序。我想在启动画面时执行此下载操作。我已经创建了一个启动画面并在其中编写了下载代码,但我的问题是启动画面直到下载结束才显示。 任何帮助将不胜感激。

作为参考,我已在下面包含了启动画面活动类。

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);
}

}

3 个答案:

答案 0 :(得分:1)

中显示启动画面
protected void onPreExecute()

回调 并将其隐藏在

protected void onPostExecute(String result)

答案 1 :(得分:1)

尝试添加空白onPreExecution: -

@Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

答案 2 :(得分:0)

这里的后台操作没用,因为你在调用downloadUrl(new VolleyCallback()

时正在启动另一个线程

在调用下载回调后必须调用intent,因此在onSuccessResponse

结束时移动它

当您使用自己的线程执行可以控制的操作时使用异步任务,例如缓冲例如

试试这段代码

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);
}