在网络下载时创建android启动屏幕



我创建了一个android应用程序,可以从web服务下载数据库记录。我希望在启动屏幕出现时执行此下载操作。我已经创建了一个启动屏幕,并在其中编写了下载代码,但我的问题是启动屏幕直到下载结束才显示。任何帮助都将不胜感激。

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

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

}

在中显示启动屏幕

protected void onPreExecute()

回调并将其隐藏在中

protected void onPostExecute(String result)

只需尝试在PreExecution:-上添加空白

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

这里的后台操作是无用的,因为在调用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);
}

相关内容

  • 没有找到相关文章

最新更新