在android中运行timerTask时出现RunTimeException

发布于 2024-12-07 03:41:13 字数 1726 浏览 1 评论 0原文

在我的 android 服务中,我正在运行一个简单的计时器,但 android 抛出异常...

我的代码如下

public class MyService extends Service{
   private Timer timer = new Timer();

   public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

      timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
        func1();
        }
        }, 0, UPDATE_INTERVAL);

   }
}


void func1(){
    Log.i(TAG,"Just printing");
}

但我的代码抛出这样的异常

09-30 11:56:54.297: ERROR/AndroidRuntime(414): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-30 11:56:54.297: ERROR/AndroidRuntime(414):     at android.os.Handler.<init>(Handler.java:121)
09-30 11:56:54.297: ERROR/AndroidRuntime(414):     at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:173)
09-30 11:56:54.297: ERROR/AndroidRuntime(414):     at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:173)
09-30 11:56:54.297: ERROR/AndroidRuntime(414):     at android.location.LocationManager._requestLocationUpdates(LocationManager.java:579)
09-30 11:56:54.297: ERROR/AndroidRuntime(414):     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:446)
09-30 11:56:54.297: ERROR/AndroidRuntime(414):     at com.example.mobiletracker.MTrackerService.func1(MTrackerService.java:167)
09-30 11:56:54.297: ERROR/AndroidRuntime(414):     at com.example.mobiletracker.MTrackerService$4.run(MTrackerService.java:118)
09-30 11:56:54.297: ERROR/AndroidRuntime(414):     at java.util.Timer$TimerImpl.run(Timer.java:284)

任何人都可以为我提供任何解决方案吗?

In my android service I am running a simple timer but android throws exception...

My code as follows

public class MyService extends Service{
   private Timer timer = new Timer();

   public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

      timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
        func1();
        }
        }, 0, UPDATE_INTERVAL);

   }
}


void func1(){
    Log.i(TAG,"Just printing");
}

But my code throws exception like this

09-30 11:56:54.297: ERROR/AndroidRuntime(414): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-30 11:56:54.297: ERROR/AndroidRuntime(414):     at android.os.Handler.<init>(Handler.java:121)
09-30 11:56:54.297: ERROR/AndroidRuntime(414):     at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:173)
09-30 11:56:54.297: ERROR/AndroidRuntime(414):     at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:173)
09-30 11:56:54.297: ERROR/AndroidRuntime(414):     at android.location.LocationManager._requestLocationUpdates(LocationManager.java:579)
09-30 11:56:54.297: ERROR/AndroidRuntime(414):     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:446)
09-30 11:56:54.297: ERROR/AndroidRuntime(414):     at com.example.mobiletracker.MTrackerService.func1(MTrackerService.java:167)
09-30 11:56:54.297: ERROR/AndroidRuntime(414):     at com.example.mobiletracker.MTrackerService$4.run(MTrackerService.java:118)
09-30 11:56:54.297: ERROR/AndroidRuntime(414):     at java.util.Timer$TimerImpl.run(Timer.java:284)

Can anybody please provide me any solution ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文