【问题标题】:Android Calling Multiple methods 1 by 1Android 调用多个方法 1 by 1
【发布时间】:2018-01-06 04:50:30
【问题描述】:

我有这个功能,它可以检查用户所做的选择。 所以例如 有4个选择:

  1. InfoOfUp
  2. InfoOfArt
  3. 教区信息
  4. InfoOfAteneo。

所以当用户选择 InfoOfUp 和 InfoOfArt 然后在下一个活动中,我将单击一个包含函数的按钮: selected() 它将检查用户选择的项目。如果用户选择项目 InfoOfUp 它将运行一个特定的功能,如果用户选择项目 InfoOfArt 它也将运行一个特定的功能

问题是每个项目都有自己的功能,每个项目都有进度对话框,标记该功能是否已经完成。 所以用户选择了2个项目,因为同时调用了2个函数,所以出错了;

我希望函数被调用 1by1 函数等待另一个函数完成。

为避免混淆,我将方法称为函数。

 public void selected() {

     if (InfoOfUp.select == 1) {
                if (ayala == 0) {
                    ayala();
                ayala = 1;
            } else if (ayala == 1) {

            }
        }

        if (InfoOfArt.select == 1) {
            if (art == 0) {
                ArtInIsland();

                art = 1;
            } else if (art == 1) {

            }
        }

        if (InfoOfParish.select == 1) {
            if (parish == 0) {
                parish();
                parish = 1;

            } else if (parish == 1) {

            }
        }


        if (InfoOfAteneo.select == 1) {

            if (ateneo == 0) {
                ateneogallery();
                ateneo = 1;
            } else if (ateneo == 1) {

            }

        }

另外,如果函数调用,它会运行一个异步任务来获取数据。 这是我的异步任务:

public class connectAsyncTask3 extends AsyncTask<Void, Void, String> {
    private ProgressDialog progressDialog;
    private traffic traffic;
    private boolean displayDestinationDetails;
    String url;
    boolean launchDestination;

    connectAsyncTask3(String urlPass, traffic traffic, boolean displayDestinationDetails) {
        this.url = urlPass;
        this.traffic = traffic;
        this.displayDestinationDetails = displayDestinationDetails;
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        try {
            super.onPreExecute();
            progressDialog = new ProgressDialog(traffic.this);
            progressDialog.setMessage("Fetching route, Please wait...");
            progressDialog.setIndeterminate(true);
            progressDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected String doInBackground(Void... params) {
        JSONParser jParser = new JSONParser();
        String json = jParser.getJSONFromUrl(url);
        return json;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        progressDialog.hide();
        if (result != null) {
            Log.d("momo2", " : " + result);
            traffic.drawPath(result);
            speakOut();


        }

        if (displayDestinationDetails) {

            Intent i = new Intent(traffic.this, poppers.class);
            i.putExtra("currentMarker", traffic.markers.size());
            traffic.startActivity(i);


        }






    }
}

【问题讨论】:

  • 制作方法synchronized不够用?
  • @j.e.gr 我不熟悉同步。

标签: android function methods call


【解决方案1】:

经典的多线程情况。 创建两个线程,每个线程在方法相关,启动并使用

thread.join() 只有在第一次完成后才开始第二个线程。

great example here

【讨论】:

  • 我不知道您的项目是如何构建的,以确保线程必须采用相同的方法,但请查看我在链接中提供的示例以查看感受如何使用 join() 实现多线程。但主要的原则是要意识到这种情况必须由线程处理并让它们一个一个运行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
相关资源
最近更新 更多