Passing parameters to Asynctask

后端 未结 4 1426
日久生厌
日久生厌 2021-01-31 10:48

I am using Async tasks to get string from the menu activity and load up some stuff..but i am not able to do so..Am i using it in the right way and am i passing the parameters co

4条回答
  •  囚心锁ツ
    2021-01-31 11:20

    Instead of this i would do

     private class Setup extends AsyncTask {
    
        @Override
        protected Void doInBackground(String... params) {
        String identifier = params[0];
    
              if (identifier.matches("abc")) {
                    publishProgress(0);
                    db.insert_fri();
                } else if ((identifier.matches("xyz"))) {
                    publishProgress(1);
                    db.insert_met();
                }
            }
            return null;
        }
    
        @Override
        protected void onProgressUpdate(Integer... i) {
            // start the song here
            if (i[0] == 0) {
                song.setLooping(true);
                song.start();
            }
        }
    
        @Override
        protected void onPostExecute(Void res) {
    
        }
    
        @Override
        protected void onPreExecute() {
            // do something before execution
        }
    }
    

    and check for "identifier" before invoking the asynctask to prevent overhead of creating a AsyncTask

    like this

    if (!(getIntent().getExtras().isEmpty())) {
                    Bundle gotid = getIntent().getExtras();
                    identifier = gotid.getString("key");
                   new Setup().execute(identifier);
        }
    

提交回复
热议问题