Android Asynctask passing a single string

前端 未结 2 1237
孤街浪徒
孤街浪徒 2021-02-07 04:56

I would like to pass a single string into an asynctask. Could anyone show me how it is done? my getEntity needs The method getEntity(Activity, String, EntityGetListener) but I k

2条回答
  •  鱼传尺愫
    2021-02-07 05:13

    You can build AsyncTask with a constructor.

    public class RemoteDataTask extends AsyncTask {
    
        private String data;
    
        public RemoteDataTask(String passedData) {
            data = passedData;
        }
    
        @Override
        protected String doInBackground(Context... params) {
            // you can access "data" variable here.
            EntityUtils.getEntity(activity, params, new EntityGetListener() {
                @Override
                public void onGet(Entity entity) {
                    viewcount = entity.getEntityStats().getViews();
                }
                @Override
                public void onError(SocializeException error) {
                }
            });
            return null;
        }
    }
    

    In the application (Activity, Service etc), you can use;

    private RemoteDataTask mTask;
    private void doStuff(){
        String pass = "meow"; // story.get(position).getEntity();
        mTask = new RemoteDataTask(pass);
        mTask.execute();
    }
    

提交回复
热议问题