Use asynctask to call a function in android

后端 未结 3 800
误落风尘
误落风尘 2021-01-21 16:55

As below code shows I have a function named record() I want to call this function with asynctask but I do not know how to work with asynctask, record function takes

相关标签:
3条回答
  • 2021-01-21 17:31

    Try this-

     public class Record extends Activity {
    
            MediaPlayer mp;
            String name;
    
            public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.record);
            mp = new MediaPlayer();
            new Task1().execute();  
    
        }
    
    
    class Task1 extends AsyncTask<Void, Void, String> {
    
                @Override
                protected void onPreExecute() 
                {
                super.onPreExecute();
                }
                @Override
                protected String doInBackground(Void... arg0)
                {
                   //Record method 
                }
    
                @Override
                protected void onPostExecute(String result) 
                {
                    super.onPostExecute(result);
    
                }
            }
        }
    
    0 讨论(0)
  • 2021-01-21 17:31

    Make AsyncTask Class and write code there and call your AsyncTask Class by new AsyncTaskClass().execute();

    0 讨论(0)
  • 2021-01-21 17:36

    Try below code for your reference.

    public void record() {

       new AsyncTask<Void, Void, Void() {
    
    @Override
                protected void doInBackground(Void... params) {
    }
    Override
                protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
    
                }
    
    
    }.execute();
      }
    
    0 讨论(0)
提交回复
热议问题