1

Well what I am trying to do, is to make a function run every,let's say, 10 seconds. Now also I'm trying to make this function stops when the user press on an exit button which finish the activity. But if let's say the user press on home or back , when he's at the activity, then I want the function to run in the background.

In order to do so I've done the next few steps -

I've declared on the activity at the manifest as follows -

       <activity android:name="com.example.one.ViewList" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" >
            </action>

            <category android:name="android.intent.category.NORMAL" >
            </category>
        </intent-filter>
    </activity>

At the code of this ViewList activity, I've use the next code at the onResume function -

        myTimer = new Timer();
    MyTimerTask myTimerTask= new MyTimerTask();

    myTimer.scheduleAtFixedRate(myTimerTask, 0, 10000); 

I've also added the next class at the activity code -

    private class MyTimerTask extends TimerTask {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
               //code to get and send location information
                    Log.d("MY TIMER TASK", "RUNNING");
            }
        });
    }
}

Now at the onClick of the exit button, I've sdded the next code -

    myTimer.cancel();
  finish():

The interesting thing is - when the activity is running and I pressed on the exit button, then the function is sure stopping.

When I'm at the activity and press back or home buttons - the function is running as I wanted it to run, at the background.

But when I come back to the activity and press on the exit button the function keeps on running at the background, even though that the app was closed.

So how can I stop this function when I press on a button and not when destroying the activity?

Thanks for any kind of help

4this
  • 759
  • 4
  • 13
  • 27

1 Answers1

0

This code is tested and is working:

public class MainActivity extends Activity implements  OnClickListener {

private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button startB;
public TextView text;
private final long startTime = 30 * 1000;
private final long interval = 1 * 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startB = (Button) this.findViewById(R.id.button);
        startB.setOnClickListener(this);
        text = (TextView) this.findViewById(R.id.timer);
        countDownTimer = new MyCountDownTimer(startTime, interval);
        text.setText(text.getText() + String.valueOf(startTime/1000));
    }
    @Override
    public void onClick(View v) {
    if (!timerHasStarted) {
    countDownTimer.start();
    timerHasStarted = true;
    startB.setText("STOP");
    } else {
    countDownTimer.cancel();
    timerHasStarted = false;
    startB.setText("RESTART");
    }
    }

public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}

@Override
public void onFinish() {
text.setText("Time's up!");
}

@Override
public void onTick(long millisUntilFinished) {
text.setText("" + millisUntilFinished/1000);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

this is the xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FF0000" >

<TextView
android:id="@+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:paddingRight="10dip"
android:textSize="50dp" />

<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Start" />

</RelativeLayout>
halfer
  • 19,824
  • 17
  • 99
  • 186
Pihu
  • 1,041
  • 11
  • 35
  • Hi, Thanks for helping. But i don't understand where should I put the things that i want it to do- Is it at onTick? – 4this Apr 11 '14 at 10:11
  • I didn't understand what you mean in that. Also this is countdown, when it finished it wouldn't start again - and i need something that every few secinds will be run again – 4this Apr 11 '14 at 10:34