0
我对Android(和移动)编程非常新颖。 我试图通过每分钟设置一个GPS位置来测试我的应用程序。 尝试这样做,使用ScheduledExecutorService的在android中设置测试任务的位置
所以这是我运行的类:
public class LocationTestScheduledTask implements Runnable {
private Activity activity;
public LocationTestScheduledTask(Activity a)
{
activity = a;
}
@Override
public void run() {
LocationManager locationManager =
(LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);
locationManager.addTestProvider("Test", false, false, false, false, false, false, false, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
locationManager.setTestProviderEnabled("Test", true);
// Set up your test
Location location = new Location("Test");
Random rand = new Random();
location.setLatitude(rand.nextDouble());
location.setLongitude(rand.nextDouble());
locationManager.setTestProviderLocation("Test", location);
// Check if your listener reacted the right way
locationManager.removeTestProvider("Test");
}
}
在我这是怎么调用任务从我activity.onCreate():
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
final Runnable locationTest = new LocationTestScheduledTask(this);
final ScheduledFuture sched=
scheduler.scheduleAtFixedRate(locationTest , 10, 10, TimeUnit.SECONDS);
我我可能在这里做了非常错误的事情,因为我没有看到任何位置变化。
我也试着做一些与TimerTask非常相似的东西,但没有结果。
任何人都可以指向我的代码是什么问题?
感谢您的回答! postDelayed()已经完美地用于调度任务,但是我的setTestProviderLocation有一些问题。我在这里找到了解决方案 - http://groups.google.com/group/android-developers/browse_thread/thread/07e56400d349817b?pli=1,现在它工作! – Ginandi