2011-08-31 54 views
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非常相似的东西,但没有结果。

任何人都可以指向我的代码是什么问题?

回答

1

您可以使用处理程序。我会说这会使事情更容易:

Handler handler = new Handler(); 
Runnable locationTest = new LocationTestScheduledTask(this); 

handler.postDelayed(locationTest, 1000*60); // 1000 miliseconds * 60 miliseconds = 1 minute 

你会把handler.postDelayed(...)在你的onCreate以及在你的类的身体,如果你想改变位置的每一分钟。

您可以做的更容易的事情是在模拟器上运行您的应用程序,然后转到Eclipse中的DDMS,然后您可以发送设备gps纬度和经度坐标。

您可以看到发送坐标个图像下面:

enter image description here

+0

感谢您的回答! postDelayed()已经完美地用于调度任务,但是我的setTestProviderLocation有一些问题。我在这里找到了解决方案 - http://groups.google.com/group/android-developers/browse_thread/thread/07e56400d349817b?pli=1,现在它工作! – Ginandi