在android中运行timerTask时出现RunTimeException
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论