Passing or declaring variable of AsyncExecutorService in right way

90 Views Asked by At

Since Android 11 AsyncTask is deprecated so I implemented in a separate .java file this AsyncExecutorService abstract class:

public abstract class AsyncTaskExecutorService<Params, Progress, Result> {
    private ExecutorService executor;
    private android.os.Handler handler;

    protected AsyncTaskExecutorService() {
        executor = Executors.newSingleThreadExecutor(r -> {
            Thread t = new Thread(r);
            t.setDaemon(true);
            return t;
        });
    }

    public ExecutorService getExecutor() {  return executor;  }

    public Handler getHandler() {
        if (handler == null) {
            synchronized (AsyncTaskExecutorService.class) {
                handler = new Handler(Looper.getMainLooper()) {
                };
            }
        }
        return handler;
    }

    protected void onPreExecute() { }

    protected abstract String doInBackground(Params params);

    protected abstract void onPostExecute(String result);

    protected void onProgressUpdate(@NotNull Progress value) { }

    public void publishProgress(@NotNull Progress value) {  getHandler().post(() -> onProgressUpdate(value));  }

    public void execute() { execute(null); }

    public void execute(Params params) {
        getHandler().post(() -> {
            onPreExecute();
            executor.execute(() -> {
                String result = doInBackground(params);
                getHandler().post(() -> onPostExecute(result));
            });
        });
    }

    public void shutDown() {
        if (executor != null) {
            executor.shutdownNow();
        }
    }

    public boolean isCancelled() { return executor == null || executor.isTerminated() || executor.isShutdown(); }
}

so I modified my ex-AsyncTask class into:

public class retrieveMyString extends AsyncTaskExecutorService <Object, Void, String> {   **<--- ERROR: Class 'retrieveMyString' must either be declared abstract or implement abstract method 'doInBackground(Params)' in 'AsyncTaskExecutorService'**


    private String myString;
    private String data;
    private String hostName;    

    public retrieveMyString(String passedData, Context passedContext) {
        data = passedData;
        mContext = passedContext;
    }

    @Override **<---- ERROR: Method does not override method from its superclass**
    protected String doInBackground(Void params) {
        [...]
        
        return myString;
    }

    @Override
    protected void onPostExecute(String returnedmyString) {
        //super.onPostExecute(returnedmyString);
    }
    
}

and with this class I try just to obtain a string in the moment of its declaration:

retrieveMyString retrieveOwnString = new retrieveMyString(StringToResolve, getContext());

shall I have just to change all Params params to String in the abstract class or shall I have to threat that params as String to work to obtain a string? Thanks to all in advance!! Cheers!

0

There are 0 best solutions below