如何在主线程中等待改装请求后台任务完成?

时间:2017-12-04 11:36:19

标签: java android multithreading

checkUpdate(last_update, SplashActivity.this);
Intent l = new Intent(SplashActivity.this, MainActivity.class);
startActivity(l);

这是checkUpdate方法中的代码

private void checkUpdate(String last_update, final Context context) {

    mydb = new DatabaseHelper(context);

    Cursor res = mydb.get_options();
    res.moveToFirst();
    if (((res != null) && (res.getCount() > 0))) {
        String dbLastUpdate = res.getString(0);
        if (!last_update.equals(dbLastUpdate)) {
            // Toast.makeText(context, "Database will be updated ", Toast.LENGTH_SHORT).show();
            db_Request(context);
        } else {
            Intent l = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(l);
        }
    } else {
        //Toast.makeText(context, "Database", Toast.LENGTH_SHORT).show();
        db_Request(context);

    }

    return;
}

private void db_Request(final Context context) {

    dialog = CustomProgressDialog.showProgressDialog(context, "Loading...");
    mydb = new DatabaseHelper(context);
    apiInterface = ApiClient.getClient().create(ApiInterface.class);
    Call<DatabaseModel> call = apiInterface.providers();
    call.enqueue(new Callback<DatabaseModel>() {
        @Override
        public void onResponse(Call<DatabaseModel> call, Response<DatabaseModel> response) {


            DatabaseModel result = response.body();
            if (result != null) {

                Boolean status = response.body().isStatus();
                String msg = response.body().getMsg();
                if (status.equals(true)) {

                    mydb.delete();
                    DatabaseModel.Data data = response.body().getData();
                    List<DatabaseModel.Service_parameters> params = data.getService_parameters();
                    for (int i = 0; i < params.size(); i++) {
                        int id = params.get(i).getId();
                        String service_type = params.get(i).getService_type();
                        int external_system_id = params.get(i).getExternal_system_id();
                        int payment_service_id = params.get(i).getPayment_service_id();
                        String name_ar = params.get(i).getName_ar();
                        String name_en = params.get(i).getName_en();
                        int position = params.get(i).getPosition();
                        String visible = params.get(i).getVisible();
                        String required = params.get(i).getRequired();
                        String type = params.get(i).getType();
                        String is_client_id = params.get(i).getIs_client_id();
                        String default_value = params.get(i).getDefault_value();
                        int min_length = params.get(i).getMin_length();
                        int max_length = params.get(i).getMax_length();

                        mydb.insert_service_parameters(context, id, service_type, external_system_id, payment_service_id, name_ar, name_en,
                                position, visible, required, type, is_client_id, default_value, min_length, max_length);

                    }



                    List<DatabaseModel.Service_provider_categories> cat = data.getService_provider_categories();
                    for (int i = 0; i < cat.size(); i++) {
                        int id = cat.get(i).getId();
                        String name_ar = cat.get(i).getName_ar();
                        String name_en = cat.get(i).getName_en();
                        String description_ar = cat.get(i).getDescription_ar();
                        String description_en = cat.get(i).getDescription_en();
                        String icon = cat.get(i).getIcon();

                        mydb.insert_service_provider_categories(context, id, name_ar, name_en, description_ar, description_en, icon);


                    }


                    List<DatabaseModel.Service_providers> prov = data.getService_providers();
                    for (int i = 0; i < prov.size(); i++) {
                        int id = prov.get(i).getId();
                        int service_provider_category_id = prov.get(i).getService_provider_category_id();
                        String name_ar = prov.get(i).getName_ar();
                        String name_en = prov.get(i).getName_en();
                        String description_ar = prov.get(i).getDescription_ar();
                        String description_en = prov.get(i).getDescription_en();
                        String logo = prov.get(i).getLogo();

                        mydb.insert_service_providers(context, id, service_provider_category_id, name_ar, name_en, description_ar,
                                description_en, logo);

                    }


                    List<DatabaseModel.Services> serv = data.getServices();
                    for (int i = 0; i < serv.size(); i++) {
                        int id = serv.get(i).getId();
                        int service_provider_id = serv.get(i).getService_provider_id();
                        String name_ar = serv.get(i).getName_ar();
                        String name_en = serv.get(i).getName_en();
                        String description_ar = serv.get(i).getDescription_ar();
                        String description_en = serv.get(i).getDescription_en();
                        String icon = serv.get(i).getIcon();

                        mydb.insert_services(context, id, service_provider_id, name_ar, name_en, description_ar, description_en,
                                icon);

                    }


                    List<DatabaseModel.Options> opts = data.getOptions();

                    String name = opts.get(0).getName();
                    String value = opts.get(0).getValue();

                    mydb.insert_options(context, name, value);

                    Intent l = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(l);

                    dialog.dismiss();
                }

            } else {

                 /*   CustomAlertDialog.getInstance().showInfoDialog(context.getString(R.string.error), context.getString(R.string.errorConnectingServer),
                        context.getString(R.string.close),
                        context, 1);*/
            }

        }

        @Override
        public void onFailure(Call<DatabaseModel> call, Throwable t) {
      /*      CustomAlertDialog.getInstance().showInfoDialog(context.getString(R.string.error), context.getString(R.string.errorConnectingServer),
                    context.getString(R.string.close),
                    context, 1);*/
            dialog.dismiss();
        }
    });

}

正如您所看到的,我使用Retrofit库将请求发送到checkUpdate方法内的服务器,根据Retrofit库,它是在后台线程中完成的。但问题是主线程仍在运行,它在后台任务中运行的checkUpdate方法完成之前到达startActivity方法。

如何强制主线程等待后台任务完成?

0 个答案:

没有答案