[英]Using Timer to call AsyncTask class
I'm trying to use a timer that calls a Class "AysncTask" in a fixed schedule . 我正在尝试使用一个计时器,该计时器以固定的时间表调用类“ AysncTask”。 This timer/class doesn't have anything to do with the GUI or it's elements .
此计时器/类与GUI或其元素无关。 When calling the class without timer it works , but with timer I get those errors :
当在没有计时器的情况下调用该类时,它可以工作,但是在使用计时器的情况下,我得到了以下错误:
01-04 08:39:39.074: E/AndroidRuntime(394): FATAL EXCEPTION: main
01-04 08:39:39.074: E/AndroidRuntime(394): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projecta/com.example.projecta.MainActivity}: java.lang.RuntimeException: Only one Looper may be created per thread
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.os.Looper.loop(Looper.java:123)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-04 08:39:39.074: E/AndroidRuntime(394): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 08:39:39.074: E/AndroidRuntime(394): at java.lang.reflect.Method.invoke(Method.java:521)
01-04 08:39:39.074: E/AndroidRuntime(394): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-04 08:39:39.074: E/AndroidRuntime(394): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-04 08:39:39.074: E/AndroidRuntime(394): at dalvik.system.NativeStart.main(Native Method)
01-04 08:39:39.074: E/AndroidRuntime(394): Caused by: java.lang.RuntimeException: Only one Looper may be created per thread
01-04 08:39:39.074: E/AndroidRuntime(394): at android.os.Looper.prepare(Looper.java:73)
01-04 08:39:39.074: E/AndroidRuntime(394): at com.example.projecta.MainActivity.onCreate(MainActivity.java:43)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-04 08:39:39.074: E/AndroidRuntime(394): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-04 08:39:39.074: E/AndroidRuntime(394): ... 11 more
This is my code so far : 到目前为止,这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Timer timer = new Timer();
timer.schedule(new UpdateTimeTask(),1, 3000);
class UpdateTimeTask extends TimerTask {
public void run()
{
// do stufff
new status().execute();
}
}
So any idea how to solve this issue ? 那么有什么想法如何解决这个问题? and thanks in advance
并预先感谢
Finally found a solution : 终于找到了解决方案:
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
new status().execute();
// PerformBackgroundTask this class is the class that extends AsynchTask
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 10000); //execute in every 50000 ms
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.