任何人都可以帮助我吗?我显然做错了,我似乎无法弄清楚自己。基本上 doInBackground()方法没有运行。
这是整个类文件和调用它的代码行
呼叫:
new MyTimer().execute();
文件:
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);
}
}
编辑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
答案 0 :(得分:1)
试试这个..
公共类MyAsyncTask扩展了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
}
}
答案 1 :(得分:1)
您在线程中启动线程(计时器是一个线程),这是一个问题。 我发现你需要可重复的代码。为什么不使用没有AsyncTask的Timer?它是单独的线程,它不会挂起UI。当然,您需要Handler对象来更新任何UI,因为您无法从另一个UI更新UI,而不能更新UI线程。 这样的事情应该有效:
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);
答案 2 :(得分:1)
您可以在后台使用onProgressUpdate
更改TextView文本。
@Override
protected void onProgressUpdate(String... values)
{
GPSMain.timer.setText("Time Taken: "+values[0]);
super.onProgressUpdate(values);
}
并在publishProgress(timeTakenString)
方法中调用此doinBackground
。
有关AsyncTask的更多详细信息,请click here