Running two classes at the same time in android studioIs there a way to run Python on Android?How to check if a service is running on Android?Run/install/debug Android applications over Wi-Fi?Get current time and date on AndroidWhy is subtracting these two times (in 1927) giving a strange result?How do I add a library project to Android Studio?Android Studio: Add jar as library?“cannot resolve symbol R” in Android StudioOverriding a specific non-abstract function from class depending on a certain situationKill Network Service Discovery from AsyncTask when done without leaks

Contradiction proof for inequality of P and NP?

Help with my training data

How to pronounce 'c++' in Spanish

Can I criticise the more senior developers around me for not writing clean code?

Prove that the countable union of countable sets is also countable

How do I produce this symbol: Ϟ in pdfLaTeX?

My bank got bought out, am I now going to have to start filing tax returns in a different state?

Work requires me to come in early to start computer but wont let me clock in to get paid for it

Could moose/elk survive in the Amazon forest?

Why did C use the -> operator instead of reusing the . operator?

What is the most expensive material in the world that could be used to create Pun-Pun's lute?

All ASCII characters with a given bit count

How do I reattach a shelf to the wall when it ripped out of the wall?

"The cow" OR "a cow" OR "cows" in this context

Can a level 2 Warlock take one level in rogue, then continue advancing as a warlock?

How exactly does Hawking radiation decrease the mass of black holes?

I preordered a game on my Xbox while on the home screen of my friend's account. Which of us owns the game?

Find a stone which is not the lightest one

What to do with someone that cheated their way through university and a PhD program?

What is this word supposed to be?

How important is it that $TERM is correct?

Why do distances seem to matter in the Foundation world?

What is the best way to deal with NPC-NPC combat?

How to have a sharp product image?



Running two classes at the same time in android studio


Is there a way to run Python on Android?How to check if a service is running on Android?Run/install/debug Android applications over Wi-Fi?Get current time and date on AndroidWhy is subtracting these two times (in 1927) giving a strange result?How do I add a library project to Android Studio?Android Studio: Add jar as library?“cannot resolve symbol R” in Android StudioOverriding a specific non-abstract function from class depending on a certain situationKill Network Service Discovery from AsyncTask when done without leaks






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















The way that I have my current application set up is that when you press the button then the app displays a counter on the bottom that goes up to ten then stops and then it displays a counter on the top that goes from 10 to 0. However, what I need to do is make these counters happen at the same time. I tried using threads but I think that I must not have been doing it right. Any help would be appreciated.



edit: I want to run mytask and mytask1 at the same time, they currently run after each other



edit2: I was asked for the code for publish progress



protected final void publishProgress(Progress... values) 
if (!isCancelled())
getHandler().obtainMessage(MESSAGE_POST_PROGRESS,
new AsyncTaskResult<Progress>(this, values)).sendToTarget();




Code:



public class MainActivity extends AppCompatActivity 

Button btn;
TextView txt;
Integer count =1;
Integer count1 =10;
TextView txt1;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//p
//p
btn = (Button) findViewById(R.id.button);
btn.setText("Start");
txt = (TextView) findViewById(R.id.textView);
txt1 = (TextView) findViewById(R.id.textView2);
View.OnClickListener listener = new View.OnClickListener()
public void onClick(View view)
count =1;
//p
//p
switch (view.getId())
case R.id.button:
new MyTask().execute(10);
new MyTask1().execute(0);
break;


;
btn.setOnClickListener(listener);




class MyTask extends AsyncTask<Integer, Integer, String>
@Override
protected String doInBackground(Integer... params)
for (; count <= params[0]; count++)
try
Thread.sleep(1000);
publishProgress(count);
catch (InterruptedException e)
e.printStackTrace();


return "Task Completed.";

@Override
protected void onPostExecute(String result)
txt.setText(result);
btn.setText("Restart");

@Override
protected void onPreExecute()
txt.setText("Task Starting...");

@Override
protected void onProgressUpdate(Integer... values)
txt.setText("BackGround Task Running..."+ values[0]);



class MyTask1 extends AsyncTask<Integer, Integer, String>
@Override
protected String doInBackground(Integer... params)
for (; count1 >= params[0]; count1--)
try
Thread.sleep(1000);
publishProgress(count1);
catch (InterruptedException e)
e.printStackTrace();


return "Task Completed.";

@Override
protected void onPostExecute(String result)
txt1.setText(result);
btn.setText("Restart");

@Override
protected void onPreExecute()
txt1.setText("Task Starting...");

@Override
protected void onProgressUpdate(Integer... values)
txt1.setText("Countdown "+ values[0]);













share|improve this question
























  • please add your code for the publishProgress function

    – Omri Attiya
    Mar 22 at 17:13











  • @OmriAttiya I found it edited

    – Jenny Nicky
    Mar 22 at 17:23


















0















The way that I have my current application set up is that when you press the button then the app displays a counter on the bottom that goes up to ten then stops and then it displays a counter on the top that goes from 10 to 0. However, what I need to do is make these counters happen at the same time. I tried using threads but I think that I must not have been doing it right. Any help would be appreciated.



edit: I want to run mytask and mytask1 at the same time, they currently run after each other



edit2: I was asked for the code for publish progress



protected final void publishProgress(Progress... values) 
if (!isCancelled())
getHandler().obtainMessage(MESSAGE_POST_PROGRESS,
new AsyncTaskResult<Progress>(this, values)).sendToTarget();




Code:



public class MainActivity extends AppCompatActivity 

Button btn;
TextView txt;
Integer count =1;
Integer count1 =10;
TextView txt1;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//p
//p
btn = (Button) findViewById(R.id.button);
btn.setText("Start");
txt = (TextView) findViewById(R.id.textView);
txt1 = (TextView) findViewById(R.id.textView2);
View.OnClickListener listener = new View.OnClickListener()
public void onClick(View view)
count =1;
//p
//p
switch (view.getId())
case R.id.button:
new MyTask().execute(10);
new MyTask1().execute(0);
break;


;
btn.setOnClickListener(listener);




class MyTask extends AsyncTask<Integer, Integer, String>
@Override
protected String doInBackground(Integer... params)
for (; count <= params[0]; count++)
try
Thread.sleep(1000);
publishProgress(count);
catch (InterruptedException e)
e.printStackTrace();


return "Task Completed.";

@Override
protected void onPostExecute(String result)
txt.setText(result);
btn.setText("Restart");

@Override
protected void onPreExecute()
txt.setText("Task Starting...");

@Override
protected void onProgressUpdate(Integer... values)
txt.setText("BackGround Task Running..."+ values[0]);



class MyTask1 extends AsyncTask<Integer, Integer, String>
@Override
protected String doInBackground(Integer... params)
for (; count1 >= params[0]; count1--)
try
Thread.sleep(1000);
publishProgress(count1);
catch (InterruptedException e)
e.printStackTrace();


return "Task Completed.";

@Override
protected void onPostExecute(String result)
txt1.setText(result);
btn.setText("Restart");

@Override
protected void onPreExecute()
txt1.setText("Task Starting...");

@Override
protected void onProgressUpdate(Integer... values)
txt1.setText("Countdown "+ values[0]);













share|improve this question
























  • please add your code for the publishProgress function

    – Omri Attiya
    Mar 22 at 17:13











  • @OmriAttiya I found it edited

    – Jenny Nicky
    Mar 22 at 17:23














0












0








0








The way that I have my current application set up is that when you press the button then the app displays a counter on the bottom that goes up to ten then stops and then it displays a counter on the top that goes from 10 to 0. However, what I need to do is make these counters happen at the same time. I tried using threads but I think that I must not have been doing it right. Any help would be appreciated.



edit: I want to run mytask and mytask1 at the same time, they currently run after each other



edit2: I was asked for the code for publish progress



protected final void publishProgress(Progress... values) 
if (!isCancelled())
getHandler().obtainMessage(MESSAGE_POST_PROGRESS,
new AsyncTaskResult<Progress>(this, values)).sendToTarget();




Code:



public class MainActivity extends AppCompatActivity 

Button btn;
TextView txt;
Integer count =1;
Integer count1 =10;
TextView txt1;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//p
//p
btn = (Button) findViewById(R.id.button);
btn.setText("Start");
txt = (TextView) findViewById(R.id.textView);
txt1 = (TextView) findViewById(R.id.textView2);
View.OnClickListener listener = new View.OnClickListener()
public void onClick(View view)
count =1;
//p
//p
switch (view.getId())
case R.id.button:
new MyTask().execute(10);
new MyTask1().execute(0);
break;


;
btn.setOnClickListener(listener);




class MyTask extends AsyncTask<Integer, Integer, String>
@Override
protected String doInBackground(Integer... params)
for (; count <= params[0]; count++)
try
Thread.sleep(1000);
publishProgress(count);
catch (InterruptedException e)
e.printStackTrace();


return "Task Completed.";

@Override
protected void onPostExecute(String result)
txt.setText(result);
btn.setText("Restart");

@Override
protected void onPreExecute()
txt.setText("Task Starting...");

@Override
protected void onProgressUpdate(Integer... values)
txt.setText("BackGround Task Running..."+ values[0]);



class MyTask1 extends AsyncTask<Integer, Integer, String>
@Override
protected String doInBackground(Integer... params)
for (; count1 >= params[0]; count1--)
try
Thread.sleep(1000);
publishProgress(count1);
catch (InterruptedException e)
e.printStackTrace();


return "Task Completed.";

@Override
protected void onPostExecute(String result)
txt1.setText(result);
btn.setText("Restart");

@Override
protected void onPreExecute()
txt1.setText("Task Starting...");

@Override
protected void onProgressUpdate(Integer... values)
txt1.setText("Countdown "+ values[0]);













share|improve this question
















The way that I have my current application set up is that when you press the button then the app displays a counter on the bottom that goes up to ten then stops and then it displays a counter on the top that goes from 10 to 0. However, what I need to do is make these counters happen at the same time. I tried using threads but I think that I must not have been doing it right. Any help would be appreciated.



edit: I want to run mytask and mytask1 at the same time, they currently run after each other



edit2: I was asked for the code for publish progress



protected final void publishProgress(Progress... values) 
if (!isCancelled())
getHandler().obtainMessage(MESSAGE_POST_PROGRESS,
new AsyncTaskResult<Progress>(this, values)).sendToTarget();




Code:



public class MainActivity extends AppCompatActivity 

Button btn;
TextView txt;
Integer count =1;
Integer count1 =10;
TextView txt1;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//p
//p
btn = (Button) findViewById(R.id.button);
btn.setText("Start");
txt = (TextView) findViewById(R.id.textView);
txt1 = (TextView) findViewById(R.id.textView2);
View.OnClickListener listener = new View.OnClickListener()
public void onClick(View view)
count =1;
//p
//p
switch (view.getId())
case R.id.button:
new MyTask().execute(10);
new MyTask1().execute(0);
break;


;
btn.setOnClickListener(listener);




class MyTask extends AsyncTask<Integer, Integer, String>
@Override
protected String doInBackground(Integer... params)
for (; count <= params[0]; count++)
try
Thread.sleep(1000);
publishProgress(count);
catch (InterruptedException e)
e.printStackTrace();


return "Task Completed.";

@Override
protected void onPostExecute(String result)
txt.setText(result);
btn.setText("Restart");

@Override
protected void onPreExecute()
txt.setText("Task Starting...");

@Override
protected void onProgressUpdate(Integer... values)
txt.setText("BackGround Task Running..."+ values[0]);



class MyTask1 extends AsyncTask<Integer, Integer, String>
@Override
protected String doInBackground(Integer... params)
for (; count1 >= params[0]; count1--)
try
Thread.sleep(1000);
publishProgress(count1);
catch (InterruptedException e)
e.printStackTrace();


return "Task Completed.";

@Override
protected void onPostExecute(String result)
txt1.setText(result);
btn.setText("Restart");

@Override
protected void onPreExecute()
txt1.setText("Task Starting...");

@Override
protected void onProgressUpdate(Integer... values)
txt1.setText("Countdown "+ values[0]);










java android multithreading






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 17:24







Jenny Nicky

















asked Mar 22 at 16:35









Jenny NickyJenny Nicky

135




135












  • please add your code for the publishProgress function

    – Omri Attiya
    Mar 22 at 17:13











  • @OmriAttiya I found it edited

    – Jenny Nicky
    Mar 22 at 17:23


















  • please add your code for the publishProgress function

    – Omri Attiya
    Mar 22 at 17:13











  • @OmriAttiya I found it edited

    – Jenny Nicky
    Mar 22 at 17:23

















please add your code for the publishProgress function

– Omri Attiya
Mar 22 at 17:13





please add your code for the publishProgress function

– Omri Attiya
Mar 22 at 17:13













@OmriAttiya I found it edited

– Jenny Nicky
Mar 22 at 17:23






@OmriAttiya I found it edited

– Jenny Nicky
Mar 22 at 17:23













1 Answer
1






active

oldest

votes


















0














AsyncTasks are executed on a single thread according to documentation, so that might be the issue.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304106%2frunning-two-classes-at-the-same-time-in-android-studio%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    AsyncTasks are executed on a single thread according to documentation, so that might be the issue.






    share|improve this answer



























      0














      AsyncTasks are executed on a single thread according to documentation, so that might be the issue.






      share|improve this answer

























        0












        0








        0







        AsyncTasks are executed on a single thread according to documentation, so that might be the issue.






        share|improve this answer













        AsyncTasks are executed on a single thread according to documentation, so that might be the issue.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 22 at 23:06









        EnividmkEnividmk

        4114




        4114





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304106%2frunning-two-classes-at-the-same-time-in-android-studio%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해