-1

I am using this routine to start a forever-function on another thread but stop it at any time or restart it. However, I get two errors on compiling(commented):

    public class MainActivity extends AppCompatActivity 
    {
            protected void onCreate(Bundle savedInstanceState) {

        StrtBtn.setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View v) {
                    // I want to start the function on another thread here
                    myTask = new MyTask();
                    myTask.execute();
                  }});

            StpBtn.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick(View v) {
                    // I want to stop the function
                    message = "StopVideo";
                    myTask.cancel(true);
                  }});

            public void MyFunction()
            {
                // whatever
            }
        }

public class MyTask extends AsyncTask<Void, Void, Void> // Error: class 'MyTask' is public, should be declared in a file named 'MyTask.Java'
{
    protected Void doInBackground(Void... params) 
    {
        while(!isCancelled())
        {
            // my code here to call the function here
        }
    }
}
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
  • Your doInBackground(); must return a integer type value. and also implement onPost (Integer result){}; where result is the value returned by doInbacground() method – HAXM Apr 03 '16 at 02:20
  • 2
    @HAXM No, the Integer value is the Progress, not the Result – OneCricketeer Apr 03 '16 at 02:24
  • That's the wrong use for an AsyncTask. You'll want a Thread. – 323go Apr 03 '16 at 05:00
  • @323go what do you mean? Is not this a thread? And why the downvote? – Khalil Khalaf Apr 04 '16 at 01:59
  • I mean exactly what I wrote. Per [documentation](http://developer.android.com/reference/android/os/AsyncTask.html), an AsyncTask is for "short operations," not ongoing background tasks. – 323go Apr 04 '16 at 03:53
  • But this is a followup question, earlier I was asking how to do a task in a new "thread" in Java, and everyone said AsyncTask. See here: http://stackoverflow.com/questions/36321684/how-to-start-a-process-on-a-new-thread-in-java-android-studio-asking-for-the-e?noredirect=1#comment60390434_36321684 and please let me know what you think, because I thought this is being done on a Thread !! – Khalil Khalaf Apr 04 '16 at 11:54
  • @323go sorry forgot to mention your name – Khalil Khalaf Apr 04 '16 at 14:06

2 Answers2

1

Your problem is that your AsyncTask declaration is not correct, try below code:

public class MyTask extends AsyncTask<Void, Void, Void>{

    protected Void doInBackground(Void... params){
           // ..
           while(NotCancelled){
            // my code to call the function here
            }
        }

        return null;
    }
}

Here's a complete example for you:

public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {

        StrtBtn.setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View v) {
                    // I want to start the function on another thread here
                    myTask = new MyTask();
                    myTask.execute();
                }
            });

    StpBtn.setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View v) {
                    // I want to stop the function
                    message = "StopVideo";
                    myTask.cancel(true);
                }
            });
    }

    public void MyFunction() {
        // whatever
    }


    private class MyTask extends AsyncTask<Void, Void, Void> // Error: class 'MyTask' is public, should be declared in a file named 'MyTask.Java'
    {
        protected Void doInBackground(Void... params) {
            while (!isCancelled()) {
                // my code here to call the function here
            }
            return null;
        }
    }
}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
Joel Min
  • 3,387
  • 3
  • 19
  • 38
1

Use Void not void as the return type of doInBackground. Like this

    class MyTask extends AsyncTask<Void,Void,Void>{


    protected Void doInBackground(Void... params){
        // Your code here
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
    }

and while calling this task simple

myTask.execute()

I am assuming You are not doing anything with the result in your onPostExecute

theJango
  • 1,100
  • 10
  • 22
  • It says error "missing return statement" once I use Void instead of void. I am not returning anything.. I want it as a while loop only.. I updated my post now – Khalil Khalaf Apr 03 '16 at 02:27
  • @FirstStep: updated answer, actually i missed it. I have the same implementation and its working like charm. – theJango Apr 03 '16 at 02:28
  • I also had to remove 'void' from myTask.execute() since 'Void' also didn't work. Now I get a new error! – Khalil Khalaf Apr 03 '16 at 02:37