Method doInBackground does not override from the superclass - android

I am trying to use AsyncTask but both my doInBackground and onPostExecute methods give me the same error saying Method does not override method from its superclass. My code is:
public class SigninActivity extends AsyncTask{
private TextView status, result;
private Context context;
private int flag = 0;
public SigninActivity(Context context, TextView status, TextView result, int flag) {
this.context = context;
this.statusField = status;
this.roleField = result;
this.flag = flag;
}
protected void onPreExecute() {
}
#Override
protected String doInBackground(String... arg0) {
//Code
}
#Override
protected void onPostExecute(String result) {
//Code
}

You should create AsyncTask with three parameters < X,Y,Z >. For more details in here
private class MyAsyncTask extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... params) {
// your code
}
#Override
protected void onPostExecute(String result) {
//your code
}
#Override
protected void onPreExecute() {
//your code
}
#Override
protected void onProgressUpdate(String... text) {
// your code
}
}

Related

AsyncTask to update ui counter which is invoked in other class in android

I need to update file upload count dynamically into Ui progressbar which is invoked in other class method. How can communicate between AsyncTask doInBackground() and myClass.uploadFiles(). please help me
this is activity
public class MyActivity extends AppCompatActivity {
private ProgressDialog dialog = null;
String status;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file);
CounteTask task = new CounteTask();
task.execute();
}
private class CounteTask extends AsyncTask<String, Integer, Void> {
#Override
protected Void doInBackground(String... params) {
try {
status = myClass.uploadFiles(MyActivity.this);
} catch (Exception e) {
dialog.dismiss();
}
return null;
}
#Override
protected void onPostExecute(Void resultGetlogin) {
dialog.dismiss();
}
protected void onPreExecute() {
dialog = ProgressDialog.show(Sync_Records.this, "", "Please wait, file count..");
}
protected void onProgressUpdate(Integer... values) {
}
}
}
MyClass.java from here need to update ui thread.
public void MyClass{
public static String uploadFiles(Context context) {
for (count=0 ; count <= 10; count++) {
try {
upload(count); //need to show each count of file which is currently uploading
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
return status;
}

How To Show Progress In Percentage For All Files

How can I implement a progress bar that will show the progress download of all files in percentage from 0% to 100% (all files). This is how my async looks like:
public class DownloadFileFromFTP extends AsyncTask<String, Void, String> {
private Context context;
private ProgressDialog progressDialog;
public DownloadFileFromFTP(Context context) {
this.context = context;
this.progressDialog = new ProgressDialog(MyActivity.this);
this.progressDialog.setCancelable(false);
this.progressDialog.setMessage("Please wait...");
this.progressDialog.show();
}
protected void onPreExecute() {
}
#Override
protected String doInBackground(String... arg0) {
//code to list ftp files
return null;
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(String result) {
if (this.progressDialog != null) {
this.progressDialog.dismiss();
}
}
}
Its should be
#Override
protected void onProgressUpdate(Void... values) {
this.progressDialog.setProgress(Integer.parseInt(values[0]));
}

Android: ProgressDialog not shown with AsyncTask

I'm trying to display a ProgressDialog in my Activity while I'm executing a task with AsyncTask. Here my code below.
My problem is that onPreExecute() is called (I checked it with print) but my progressDialog is not shown while my function sendRequest is executed in doInBackground(..). I don't understand what it happens and I don't know how to solve that. I googled it but I didn't find any suitable solution. If you have any idea it would be great for me.
private static Activity activity;
private static ProgressDialog dialog;
public MyClass(Activity activity){
this.activity = activity;
}
public static String sendRequest(String request){
//do something
}
#Override
protected void onPreExecute() {
super.onPreExecute();
dialog = ProgressDialog.show(activity,
"ProgressDialog",
"Wait!");
}
#Override
protected String doInBackground(String... params) {
String result = sendRequest(params[0]);
return result;
}
#Override
protected void onPostExecute(final String result) {
dialog.dismiss();
}
Try:
progressDialog = new ProgressDialog(context);
progressDialog.setTitle("title");
progressDialog.setMessage("message");
progrsesDialog.show();
Try this
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(CardPaymentActivity.this,
"Progress", "please wait...");
}
#Override
protected String doInBackground(String... params) {
publishProgress("sleep time..."); // Calls onProgressUpdate()
return null;
}
#Override
protected void onPostExecute(String result) {
progressDialog.dismiss();
}
#Override
protected void onProgressUpdate(String... text) {
}
}
and call AsyncTaskRunner class in your activity class like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_payment);
AsyncTaskRunner myTask = new AsyncTaskRunner();
myTask.execute();
}

Get a TextView in an AsyncTask - Android

I want to publish the result of my AsyncTask (a string) in a textView.
Here is my Main:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ReadRss readRss=new ReadRss(this);
readRss.execute();
......
}
Here is my AsyncTask:
public class ReadRss extends AsyncTask<Void,Void,Void> {
public ReadRss(Context context){
}
#Override
protected void onPreExecute() {
}
#Override
protected void onPostExecute(Void aVoid) {
}
#Override
protected Void doInBackground(Void... params) {
ProcessXml();
return null;
}
private void ProcessXml() {
//HERE CREATE MY STRING
String myresult="example";
TextView txt_ris = (TextView)findViewById(R.id.txt_ris); <---HOW CAN I DO THIS?
txt_ris.setText(myresult);
}
}
}
}
FindViewById don't work in the AsyncTask so how can i get the TextView in here?
Maybe i can pass it as a paramiter in the AsyncTask, What is the syntax?
You need to place UI work in onPostExecute method, since doInBackground executes in not UI thread
public class ReadRss extends AsyncTask<Void,Void,String> {
public ReadRss(Context context){
}
#Override
protected void onPreExecute() {
}
#Override
protected void onPostExecute(String string) {
TextView txt_ris = (TextView)findViewById(R.id.txt_ris);
txt_ris.setText(myresult);
}
#Override
protected String doInBackground(Void... params) {
return ProcessXml();
}
private String ProcessXml() {
//HERE CREATE MY STRING
return "example";
}
}
For your TextView to be correctly referenced you need a context and you already have a reference to your starting Activity in your AsyncTask constructor, so you can do something like:
public class ReadRss extends AsyncTask<Void,Void,Void> {
private TextView tv;
private YourStartingActivity activity;
public ReadRss(Context context){
activity = (YourStartingActivity)context;
tv = (TextView)activity.findViewById(R.id.txt_ris)
}
#Override
protected void onPreExecute() {
...
}
#Override
protected void onPostExecute(Void aVoid) {
(follow Michael Spitsin instructions here)
}
#Override
protected Void doInBackground(Void... params) {
...
}
}

Use the same AsyncTask to update different views

I have a button on 6 different Activities. Clicking on that button does almost the same task with different params depending on the Activity.
This will be done using an AsyncTask and in onPostExecute() the button state will be changed.
someButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new Task().execute("param1", "param2");
}
}
private class Task extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
//background task using params[0], params[1]
return "success" or "error";
}
#Override
protected void onPostExecute(String result) {
if (result == "success") {
//change the someButton state
}else{
//show an error message
}
}
Instead of having the same AsyncTask in all the 6 Activities, how can I use a single Asynctask from all the Activities and change the respective view?
You should create Task, with methods onSuccess, onFailure and override them.
public class Task extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
//background task using params[0], params[1]
return "success" or "error";
}
#Override
protected void onPostExecute(String result) {
if (result == "success") {
onSuccess(result);
}else{
onFailure(result);
}
}
protected void onSuccess(String result) {};
protected void onFailure(String result) {};
}
and then in activity use it like this:
someButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new Task(){
#Override
protected void onSuccess(String result){
// do what you want
}
#Override
protected void onFailure(String result){
// do what you want
}
}.execute("param1", "param2");
}
}
Put your Task in its own file and make it public.
Create a callback interface:
public interface TaskCallback {
public void onSuccess(String result);
public void onFailure(String errorMessage);
}
Give such a callback to your Task:
public class Task extends AsyncTask<String, Void, String> {
private TaskCallback callback;
public Task(TaskCallback callback) {
this.callback = callback;
}
#Override
protected String doInBackground(String... params) {
//background task using params[0], params[1]
return "success" or "error";
}
#Override
protected void onPostExecute(String result) {
if (result == "success") {
callback.onSuccess(result);
} else{
callback.onFailure(errorMessage);
}
}
}
And then implement the callback when creating the Task instance in your activity:
someButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
private TaskCallback callback = new TaskCallback() {
#Override
public void onSuccess(String result) {
//change the someButton state
}
#Override
public void onFailure(String errorMessage) {
//show an error message
}
}
new Task(callback).execute("param1", "param2");
}
}

Categories

Resources