开发者

Set location for test from a scheduled task in android

I am very new to Android (and mobile) programming. I am trying to test my application by setting a GPS location every minute. Trying to do that using a ScheduledExecutorService

So this is my runnable class:

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, Cr开发者_JAVA百科iteria.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");         
    }     
}

In this is how i invoke the task from my activity.onCreate():

final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
final Runnable locationTest = new LocationTestScheduledTask(this);
final ScheduledFuture sched=
        scheduler.scheduleAtFixedRate(locationTest , 10, 10, TimeUnit.SECONDS);

I am probably doing something very wrong here, since I don't see any location changes.

I have also tried to do something very similar with TimerTask, but no result there too.

Can anyone point me to whats wrong with the code?


You could use a handler. I'd say it would make things a lot easier:

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

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

you would put the handler.postDelayed(...) in your onCreate as well as in your class body if you want to change the location every minute.

Something easier that you could do would be to run your app on an Emulator, then go to DDMS in Eclipse, then you can send the device gps latitude and longitude coordinates.

You can see where to send the coordinates in th image below:

Set location for test from a scheduled task in android

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜