在我的 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)
任何人都可以为我提供任何解决方案吗?