-4

i am running a small sequence of commands in my main_Activity.

this main activity starts another activity called subActivity periodically.

now after 20 seconds the subActivity stops and returns control to main_activity.

but the problem is other commands are automatically executed in the background.

for example : Send Sms >> Send Sms >> Send Sms

i want the second Send Sms to be executed only after 20 seconds(i.e only after the control is transferred from subActivity to main_Activity)

Code for main Activity

String arr[]={"send","receive"}; 

        for (int i=0;i < arr.length;i++)
            if(arr[i].equals("send"))
                { 
                txtView.setText("sending a sms" ); 
                return_value=0;  
                sendSMS("121","START");
                Intent ii =new Intent("com.example.sendsmsdemo.WAITINGROOM");

                startActivity(ii);
                }

code of sub Activity //count down timer runs till 20 and the the finish() is called

mobman
  • 89
  • 1
  • 8
  • 1
    post your activities code – Abdul Mohsin Nov 11 '14 at 13:21
  • added ...please check it out – mobman Nov 11 '14 at 13:27
  • to achieve this you have to use [CountDownTimer](http://stackoverflow.com/questions/10032003/how-to-make-a-countdown-timer-in-android) class in Android. that will prefectly – Abdul Mohsin Nov 11 '14 at 13:32
  • I didn't downvote, yet, and I only criticized your comment because it can get you in trouble on SO. Just trying to help. As far as your question, I'm still trying to figure out exactly what you are doing and *exactly what the problem is* – codeMagic Nov 11 '14 at 13:32
  • i have used a count down timer only , you want me to use count down timer in my main Activity @AbdulMohsin – mobman Nov 11 '14 at 13:33
  • 1
    Put whatever code you want to run when the timer finishes in `onFinish()`. What's the problem? – codeMagic Nov 11 '14 at 13:34
  • @codeMagic sorry for everything !!! please try and help me – mobman Nov 11 '14 at 13:34
  • the thing is i have a batch of commands and ideally as soon as i use an intent to switch to another activity(the SubActivity) execution of main_Activity should stop. whereas it's not stopping and rather other requests are being executed – mobman Nov 11 '14 at 13:36
  • I'm sorry but you are not explaining it very well. What are these other "requests" that are being executed and where in your code are they? – codeMagic Nov 11 '14 at 13:56

3 Answers3

0

You need to schedule your tasks, with PendingIntent, ScheduledExecutorService or something similar. See this simple example.

good luck.

danizmax
  • 2,446
  • 2
  • 32
  • 41
0

Use startActivityforResult() to launch new activity from main activity and in main activity implement onActivityResult(int rq,int rc,Intent data) and send SMS from this method. This method will be called after your activity returns from new activity to main activity.

TheGraduateGuy
  • 1,450
  • 1
  • 13
  • 35
0

i Would reccomend to you use event library like Otto by square you will be able to time everything by events you create. this will help you sync all the activities you do.

itai
  • 294
  • 1
  • 14