I am super new to Android and Java development in general. Here is the basic setup: I have a splash screen with AsyncTask to check server availability. After this topic, I made a callback in my activity. This is more important than doing the work in OnPostExecute() , because I want to reuse this task in different actions.
However, in my callback I check if the status is ok. If so, he should start the next operation. But from the context of my callback, I donβt understand how I can get the Activity link that I need as a parameter for Intent.
This is the code in my OnCreate activity:
//Check server status CheckServiceTask t = new CheckServiceTask(new OnTaskCompleted<ShaggyServiceStatus>() { @Override public void onTaskCompleted(ShaggyServiceStatus result) { Log.i(TAG, "Callback. Result: " + result.getStatus()); ProgressBar pb = (ProgressBar) findViewById(R.id.splash_progress); pb.setVisibility(View.INVISIBLE); if (result.getStatusCode() == 999){ TextView t = (TextView) findViewById(R.id.splash_status_text); t.setText(result.getStatus()); return; } Intent i = new Intent(getActivity(), LoginActivity.class); startActivity(i); finish(); } }); t.execute();
The part where this fails is in getActivity() . This call is not available. Using this causes an error (which I understand, since I am in the context of OnTaskCompleted ).
For completeness, this is the interface for OnTaskCompleted:
public interface OnTaskCompleted<T> { public void onTaskCompleted(T result); }
And this is the CheckServiceTask class:
public class CheckServiceTask extends AsyncTask<Void, Void, ShaggyServiceStatus>{ private static final String TAG = "coo"; public OnTaskCompleted<ShaggyServiceStatus> listener; public CheckServiceTask (OnTaskCompleted<ShaggyServiceStatus> l){ this.listener = l; } @Override protected ShaggyServiceStatus doInBackground(Void... params) { try { Log.i(TAG, "Connecting to server...");
java android android-asynctask
Coo
source share