0

I'm making an app which reads the data from a website every interval and posting a value to the screen. I'm having difficulty with making it start and stopping it.

I can make the loop start no problem and I can stop it as well. But if I try and start it again, the program crashes.

The code is here:

    final Timer myTimer = new Timer(); 


    loop.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            MyTimerTask mytimertask = new MyTimerTask();
            myTimer.scheduleAtFixedRate(mytimertask, 0, 5000);
        }
    });

    stoploop.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            textView.setText("STOPPED");
            myTimer.cancel();

        }
    });


}

private class MyTimerTask extends TimerTask {
    @Override
    public void run() {
        // TODO Auto-generated method stub
        runOnUiThread(new Runnable(){

            public void run() {
                // TODO Auto-generated method stub
                //textView.setText("changed");
                readWebpage(text);
            }

        });

    }

}

I'm quite new to threads which is why I'm probably missing something fundamental here. Any help would be greatly appreciated.

Thank you

[edit] I should add, I adapted this from a solution here: How do I repeat a method every 10 minutes after a button press and end it on another button press

   06-29 01:38:02.958: D/AndroidRuntime(1119): Shutting down VM
   06-29 01:38:02.958: W/dalvikvm(1119): threadid=1: thread exiting with uncaught exception    (group=0x40a13300)
   06-29 01:38:02.968: E/AndroidRuntime(1119): FATAL EXCEPTION: main
   06-29 01:38:02.968: E/AndroidRuntime(1119): java.lang.IllegalStateException: Timer was canceled
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at    java.util.Timer.scheduleImpl(Timer.java:561)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at    java.util.Timer.scheduleAtFixedRate(Timer.java:528)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at com.example.beam.MainActivity$1.onClick(MainActivity.java:67)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at android.view.View.performClick(View.java:4084)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at android.view.View$PerformClick.run(View.java:16966)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at android.os.Handler.handleCallback(Handler.java:615)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at android.os.Handler.dispatchMessage(Handler.java:92)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at android.os.Looper.loop(Looper.java:137)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at android.app.ActivityThread.main(ActivityThread.java:4745)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at java.lang.reflect.Method.invokeNative(Native Method)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at java.lang.reflect.Method.invoke(Method.java:511)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
   06-29 01:38:02.968: E/AndroidRuntime(1119):  at dalvik.system.NativeStart.main(Native Method)
   06-29 01:38:03.028: W/ActivityManager(156):   Force finishing activity com.example.beam/.MainActivity
   06-29 01:38:03.039: W/WindowManager(156): Failure taking screenshot for (246x410) to layer 21010
   06-29 01:38:03.552: W/ActivityManager(156): Activity pause timeout for ActivityRecord{41366318 com.example.beam/.MainActivity}
Community
  • 1
  • 1
user1147964
  • 143
  • 2
  • 11

2 Answers2

1

Just an FYI - you might want to consider AlarmManager. It allows you to control the interval even when your app is not active. (I'm assuming you are creating widgets). It allows you to control the WAKE option, that ways your app does not drain battery.

http://developer.android.com/reference/android/app/AlarmManager.html

lumpawire
  • 468
  • 1
  • 7
  • 16
  • Thank you for this. I'm just trying to make an app which will run when you want it to and halt operation when you press the stop button. Once it's working, the interval will probably be ramped up to 15 minutes or so. – user1147964 Jun 29 '12 at 02:15
0
private class MyTimerTask extends TimerTask {
    @Override
    public void run() {

                mDialogHandler.sendEmptyMessage(DIALOG_OK);

    }

}

 private Handler mDialogHandler = new Handler(){
            public void handleMessage(android.os.Message msg) {
                    switch (msg.what) {
                    case DIALOG_OK:
                            // We are now back in the UI thread
                            //textView.setText("changed");
                            readWebpage(text);
                    }

            };
    };

You're trying to start a Thread from inside a thread which is illegal. I also added some code for your UI Thread woes.

Jack Satriano
  • 1,999
  • 1
  • 13
  • 17