异步运行2个方法

时间:2017-11-09 05:56:10

标签: android multithreading android-handler

我在onCreate中有2个方法,我需要一个接一个地运行,即...第二个方法将在第一个方法完成时启动,无论花费多少时间。请帮助我。

new Handler().postDelayed(new Runnable() {

        /*
         * Showing splash screen with a timer. This will be useful when you
         * want to show case your app logo / company
         */

        @Override
        public void run() {
            // This method will be executed once the timer is over
            // Start your app main activity

               populateList();

            // close this activity
            //finish();
        }
    }, 20000);getContactList();

populateList()方法

** public void populateList() {
Log.i("Populate List","Entered");

    Toast.makeText(this,String.valueOf(Common.selectedContactNos.size()),Toast.LENGTH_LONG).show();
   displayRecyclerAdapter = new DisplayRecyclerAdapter(DisplayContacts.this);
   LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this);
    recyclerView_contacts.setAdapter(displayRecyclerAdapter);
   recyclerView_contacts.setLayoutManager(mLinearLayoutManager);
   displayRecyclerAdapter.notifyDataSetChanged();
}**

2 个答案:

答案 0 :(得分:1)

只使用AsyncTask

调用TaskOne类 -  new TaskOne()。execute();

private class TaskOne extends AsyncTask<Void,Void,Void>
{

    @Override
    protected Void doInBackground(Void... params) {
        getContactList(); 
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        populateList();


    }
}

答案 1 :(得分:0)

使用所需的序列调用run方法内的两个方法。用以下代码替换您的代码

 new Handler().postDelayed(new Runnable() {

    /*
     * Showing splash screen with a timer. This will be useful when you
     * want to show case your app logo / company
     */

        @Override
        public void run() {
            // This method will be executed once the timer is over
            // Start your app main activity

            populateList();
            getContactList();
            // close this activity
            //finish();
        }
    }, 20000);