我目前有一个小问题:
我调用的asynktask完成后,试图在片段上显示2个按钮。为此,我正在使用此:

while (!recordi.getterminé){

    }
    terminé();


但这不是最佳选择,我的应用没有响应:(
那么,有什么解决方案可以在onPostExecute函数中调用函数terminé()吗?
谢谢。

这是我的代码:

片段:

public class FragmentEnregistrer extends Fragment {

    String path,nomfinal, ip="http://MYIP/php";
    ImageButton mrecord,mupload,mlire;
    TextView Nomm;
    ProgressBar progressbar;
    int i=2;
    EnregistrerSon recordi;
    String id;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View myView= inflater.inflate(R.layout.enregistrer, container, false);

        mrecord= (ImageButton) myView.findViewById(record);
        mupload= (ImageButton) myView.findViewById(upload);
        mlire= (ImageButton) myView.findViewById(lire);
        Nomm = (TextView) myView.findViewById(NomWhizz);
        progressbar = (ProgressBar) myView.findViewById(progressBar);
        progressbar.setVisibility(View.INVISIBLE);

        id=((MainActivity)getActivity()).getIdentifiant();

        mrecord.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity().getApplicationContext(), "Enregistrement en cours", Toast.LENGTH_SHORT).show();
                String nomm = Nomm.getText().toString();
                nomm=nomm.replace(" ", "_");
                nomm=nomm.replace("&", "");

                final String finalNomm = nomm;
                recordi = new EnregistrerSon();
                recordi.execute(finalNomm);
                terminé();






            }
        });

        mupload.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View v) {
                UploadOnServer upload = new UploadOnServer();
                progressbar.setVisibility(View.VISIBLE);
                upload.execute(path,nomfinal);
                progressbar.setVisibility(View.INVISIBLE);

                HttpGetRequest request = new HttpGetRequest();
                request.execute(ip+"/son/creation_son.php?nom_whiz="+nomfinal+"&id_createur="+id);

            }
        });

        mlire.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View v) {
                LireSonLocal local = new LireSonLocal();
                Uri myUri = Uri.parse("file://"+recordi.getAccess());
                local.execute(myUri);

            }
        });

        return myView;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void terminé(){
        Log.i("Enregistrement","terminé");
        Toast.makeText(getActivity().getApplicationContext(), "Enregistrement terminé", Toast.LENGTH_LONG).show();
        path = recordi.getAccess();
        nomfinal = recordi.getNomSansExtenssions();
        Log.i("nomfinale",nomfinal);
        mlire.setVisibility(View.VISIBLE);
        mupload.setVisibility(View.VISIBLE);
        Log.i("test","");
}

}


和asynktask:

class EnregistrerSon extends AsyncTask<String,Void,String> {
    private Boolean isRecording = false;
    private Boolean termine = false;
    private String Nom, NOM;



    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected String doInBackground(String... params) {
        NOM = params[0];
        if(!isRecording){
            //configuration
            Looper.prepare();
            MediaRecorder mr = new MediaRecorder();
            mr.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
            mr.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mr.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
            mr.setAudioChannels(1);
            mr.setAudioEncodingBitRate(1280000);
            mr.setAudioSamplingRate(9500000);
            mr.setOutputFile(getAccess());
            try {mr.prepare();}
            catch (IOException e) {e.printStackTrace();}

            //Demarage du record
            long start_time = System.currentTimeMillis(); //pendant 15 secondes
            long wait_time = 15000;
            long end_time = start_time + wait_time;
            mr.start();
            while (System.currentTimeMillis() < end_time){
                Long temps = end_time - System.currentTimeMillis();

                isRecording = true;
                //pendant 15 secondes
            }
            mr.stop();
            mr.reset();
            mr.release();
            isRecording = false;
            termine = true;
            //fin de l'enregistrement
        }
        return "lol";
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    protected void onPostExecute(String s) {
        super.onPostExecute(s);


    }

    @TargetApi(Build.VERSION_CODES.N)
    @RequiresApi(api = Build.VERSION_CODES.N)
    String getAccess(){
        MainActivity main = new MainActivity();
        if(Objects.equals(NOM, "Nom") || Objects.equals(NOM, "") || NOM == null ) {
            Calendar now = Calendar.getInstance();
            Nom = now.get(Calendar.HOUR_OF_DAY) + "_" + now.get(Calendar.DAY_OF_MONTH) + "_" + now.get(Calendar.MONTH) + "_" + now.get(Calendar.YEAR);
        }
        else{
            Nom = NOM;
        }
        File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Whizz/");
        directory.mkdirs();
        String filename = directory +"/"+Nom+".mp3";
        Log.i("filename",filename);
        return filename;
    }

    Boolean getTerminé(){
        return termine;
    }

    String getNomSansExtenssions(){
        return Nom;
    }

}

最佳答案

要在terminé类中执行方法AsyncTask。我建议有以下几点:


FragmentEnregistrer类内创建EnregistrerSon的实例字段。

private FragmentEnregistrer fragmentEnregistrer;

EnregistrerSon类中使用预期的FragmentEnregistrer类参数创建一个构造函数,并将该参数分配给field变量。

public EnregistrerSon(FragmentEnregistrer fe) {
 this.fragmentEnregistrer = fe;
}

然后,您可以在terminé方法中调用onPostExecute方法,如下所示:

protected void onPostExecute(String s) {
  super.onPostExecute(s);
  this.fragmentEnregistrer.terminé();
}

最后,在FragmentEnregistrer实例上传递EnregistrerSon对象的实例。

recordi = new EnregistrerSon(FragmentEnregistrer.this);
recordi.execute(finalNomm);

07-24 14:19