编码片段和异步任务之间的通信的问题

时间:2017-11-11 11:35:33

标签: android android-fragments interface android-asynctask

我对这个概念不熟悉。我读过几个帖子但我阻止了,所以提前感谢你的耐心等待!

在片段(frag1)中,我启动了异步任务。我想阻止用户在任务未完成时做任何事情,所以我想传达已完成任务的百分比,以便用户等待通知。

我在java SetVal.java中定义了一个接口:

interface SetVal {
    void setVal(int val);
}

我的异步任务:

class AsyncCounter extends AsyncTask<Void, Integer, Void> {
    private SetVal sender;

    public AsyncCounter(SetVal sv){
        this.sender = sv;
    }

    @Override
    protected  Void doInBackground(Void... params) {
        for(int i=0;i<60;i++){
            publishProgress(i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        Log.i("ASYNC TASK","val: "+values[0]);
        sender.setVal(values[0]);
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
    }

    public interface SetVal {
        public void setVal(int val);
    }


    public void setListener(SetVal listener) {
        this.sender = listener;
    }
}

我很难知道如何将界面传递给任务。 我的代码是否正确? 如何实例化异步任务?

片段:

public class Frag1 extends android.app.Fragment implements SetVal {
    private static TextView txt;
    private int counter;
    SetVal listener;

    public Frag1() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_frag1, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        txt = (TextView) getActivity().findViewById(R.id.txt);
        Button btn = (Button) getActivity().findViewById(R.id.btn1);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.i("Frag1", "val: " + counter);
                if (counter > 0) return;
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                Frag2 f2 = new Frag2();
                ft.replace(R.id.content_frame, f2);
                ft.addToBackStack("f2");
                ft.commit();
            }
        });

        Button btn2 = (Button) getActivity().findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AsyncCounter ac = new AsyncCounter(???????);
                ac.execute();
            }
        });

    }

    @Override
    public void setVal(int val) {
        counter = val;
    }
}

1 个答案:

答案 0 :(得分:0)

不确定是什么以及为什么要这样做,但你可以通过片段:

尝试创建一个私有方法:

public void onClick(View v) {
   startMyAsync();
}

private void startMyAsync() {
    new AsyncCounter(this).execute();
}

最后:

public AsyncCounter(Frag1 context){
    this.sender = (SetVal)context;
}