Ian
Ian

Reputation: 1490

AsyncTask not executing

can anybody help me here? I'm clearly doing soimething wrong and I can't seem to figure it out myself. Basically that the doInBackground() method is not running.

This is whe entire class file and the line of code that calls it

call:

new MyTimer().execute();

file:

public class MyTimer extends AsyncTask<Object, Object, Object> { 

Timer _timerTask = new Timer();
static int totalSeconds = 1, hour = 0, min = 0, sec = 0;
static String mTimeFormat = "%02d:%02d:%02d";
static String timeTakenString;

@Override
protected Object doInBackground(Object... params) {

        TimerTask timer = new TimerTask() {
            @Override
            public void run() {

                GPSMain.printscreen();
                totalSeconds += 1;
                sec += 1;
                if(sec >= 60) {
                    sec = 0;
                    min += 1;
                    if (min >= 60) {
                        min = 0;
                        hour += 1;
                    }
                }
                timeTakenString = String.format(mTimeFormat, hour, min, sec);
                onPostExecute(timeTakenString);

            }
        };
         (_timerTask).scheduleAtFixedRate(timer,1000,1000);
        return timeTakenString;


}
 protected void onPostExecute(String timeTaken) {
    GPSMain.timer.setText("Time Taken: "+timeTaken);
}
}

EDIT 1 :

Logcat:

06-20 10:18:20.036: DEBUG/AndroidRuntime(312): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
06-20 10:18:20.046: DEBUG/AndroidRuntime(312): CheckJNI is ON
06-20 10:18:20.225: DEBUG/AndroidRuntime(312): --- registering native functions ---
06-20 10:18:20.506: DEBUG/ddm-heap(312): Got feature list request
06-20 10:18:20.865: INFO/ActivityManager(52): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=Hartford.gps/.GPSMain }
06-20 10:18:20.925: DEBUG/AndroidRuntime(312): Shutting down VM
06-20 10:18:20.944: DEBUG/dalvikvm(312): DestroyJavaVM waiting for non-daemon threads to exit
06-20 10:18:20.944: DEBUG/dalvikvm(312): DestroyJavaVM shutting VM down
06-20 10:18:20.944: DEBUG/dalvikvm(312): HeapWorker thread shutting down
06-20 10:18:20.944: DEBUG/dalvikvm(312): HeapWorker thread has shut down
06-20 10:18:20.976: DEBUG/jdwp(312): JDWP shutting down net...
06-20 10:18:20.976: INFO/dalvikvm(312): Debugger has detached; object registry had 1 entries
06-20 10:18:20.985: ERROR/AndroidRuntime(312): ERROR: thread attach failed
06-20 10:18:20.985: DEBUG/dalvikvm(312): VM cleaning up
06-20 10:18:21.175: DEBUG/dalvikvm(312): LinearAlloc 0x0 used 638596 of 5242880 (12%)
06-20 10:18:21.495: INFO/ActivityManager(52): Displayed activity Hartford.gps/.GPSMain: 604 ms (total 24251 ms)
06-20 10:18:23.365: INFO/NotificationService(52): enqueueToast pkg=Hartford.gps callback=android.app.ITransientNotification$Stub$Proxy@44ddfa60 duration=1
06-20 10:18:23.545: DEBUG/GpsLocationProvider(52): setMinTime 250
06-20 10:18:23.545: DEBUG/GpsLocationProvider(52): startNavigating
06-20 10:18:24.206: DEBUG/LocationManager(287): removeUpdates: listener = Hartford.gps.Calculations$1@44f03dc0
06-20 10:18:24.206: DEBUG/GpsLocationProvider(52): stopNavigating
06-20 10:18:26.146: WARN/KeyCharacterMap(287): No keyboard for id 0
06-20 10:18:26.146: WARN/KeyCharacterMap(287): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-20 10:18:26.616: WARN/IInputConnectionWrapper(97): showStatusIcon on inactive InputConnection

Upvotes: 0

Views: 2480

Answers (3)

Olsavage
Olsavage

Reputation: 1108

You launch thread in a thread(timer is a thread) and that is an issue. I see you need repeatable code. Why you don't just use Timer without AsyncTask? It is separate thread and it will not hang UI. Of course you need Handler object to update any UI, because you can't update UI from another, not UI thread. Something like this should work:

Timer _timerTask = new Timer();
    TimerTask timer = new TimerTask() {
        @Override
        public void run() {
            postExecute.sendEmptyMessage(0); //first UI update              
            totalSeconds += 1;
            sec += 1;
            if(sec >= 60) {
                sec = 0;
                min += 1;
                if (min >= 60) {
                    min = 0;
                    hour += 1;
                }
            }
            timeTakenString = String.format(mTimeFormat, hour, min, sec);
            postExecute.sendEmptyMessage(1); //second UI update
        }
        private Handler postExecute = new Handler(){
            @Override
            public void dispatchMessage(Message msg) {
                super.dispatchMessage(msg);
                if(msg.what == 0)
                    GPSMain.printscreen();
                else if(msg.what == 1)
                    GPSMain.timer.setText("Time Taken: "+timeTaken);
            }
        };
    };
    _timerTask.scheduleAtFixedRate(timer,1000,1000);

Upvotes: 1

Niranj Patel
Niranj Patel

Reputation: 33238

you can use onProgressUpdate for change Text of TextView during background..

 @Override
        protected void onProgressUpdate(String... values) 
         {
            GPSMain.timer.setText("Time Taken: "+values[0]);
            super.onProgressUpdate(values);
        }

and called this publishProgress(timeTakenString) in doinBackground method.

for more detail about AsyncTask then click here

Upvotes: 1

SBK
SBK

Reputation: 1585

try this..

public class MyAsyncTask extends Activity {

public static TextView timer;
private MyTimer asyncTask;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    asyncTask = (MyTimer) new MyTimer().execute();
    timer = (TextView) findViewById(R.id.text);

}

public class MyTimer extends AsyncTask<String, Object, String> {

    Timer _timerTask = new Timer();
    int totalSeconds = 1;
    int hour = 0;
    int min = 0;
    int sec = 0;
    final String mTimeFormat = "%02d:%02d:%02d";
    String timeTakenString;
    private String timeTaken;
    @Override
    protected String doInBackground(String... params) {

        TimerTask timer = new TimerTask() {
            @Override
            public void run() {

                MyAsyncTask.printscreen();
                totalSeconds += 1;
                sec += 1;
                if (sec >= 60) {
                    sec = 0;
                    min += 1;
                    if (min >= 60) {
                        min = 0;
                        hour += 1;
                    }
                }
                timeTakenString = String
                        .format(mTimeFormat, hour, min, sec);
                onPostExecute(timeTakenString);

            }
        };
        (_timerTask).scheduleAtFixedRate(timer, 1000, 1000);
        return timeTakenString;

    }

    protected void onPostExecute(String timeTaken) {
        this.timeTaken = timeTaken;
        Message message = new Message();
        message.what = 100;
        mHandler.sendMessage(message);
    }

    Handler mHandler = new Handler() {


        public void handleMessage(Message msg) {
            if (msg.what == 100) {

                timer.setText("Time Taken: " + timeTaken);
            }
        };
    };
}

protected static void printscreen() {
    // TODO Auto-generated method stub

}

}

Upvotes: 1

Related Questions