我有一个实现LocationListener的小服务。我用我的应用程序手动测试它,它工作正常。 我还为该服务编写了一个测试用例。 我使用setTestProviderLocation期望该服务将接收位置更新。 但是,它不会发生。 有人知道什么是问题吗?我想强调的是,相同的服务在实际应用中有效。 测试用例添加在下面
package com.gkatz.android.mtg.test;
import java.util.List;
import com.gkatz.android.mtg.LocationService;
import com.gkatz.android.mtg.LocationService.LocationBinder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.test.ServiceTestCase;
public class LocationServiceTest extends ServiceTestCase<LocationService> implements LocationListener{
public LocationServiceTest() {
super(LocationService.class);
// TODO Auto-generated constructor stub
}
@Override
protected void setUp() throws Exception {
// TODO Auto-generated method stub
super.setUp();
}
public void testBinding(){
IBinder locationBinder;
locationBinder = getServiceBinder();
assertNotNull(locationBinder);
}
public void testStart(){
Intent locationIntent = new Intent(getContext(), LocationService.class);
startService(locationIntent);
}
public void testNoStart(){
LocationService locationService = getService();
assertNull(locationService);
}
public void testLocationUpdate() throws InterruptedException{
LocationBinder locationBinder;
LocationService locationService;
Context context = getContext();
LocationManager lm = getLocationManager();
context.registerReceiver(locationReceiver,
new IntentFilter("android.mtg.custom.intent.action.GPS_LOCATION"));
locationBinder = (LocationBinder)getServiceBinder();
assertNotNull(locationBinder);
locationService = getService();
assertNotNull(locationService);
Location loc = new Location(LocationManager.GPS_PROVIDER);
loc.setLongitude(4.890935);
loc.setLatitude(52.373801);
loc.setTime(System.currentTimeMillis());
lm.setTestProviderLocation(LocationManager.GPS_PROVIDER, loc);
SystemClock.sleep(3000);
loc.setLongitude(35.2276757);
loc.setLatitude(31.7765488);
loc.setTime(System.currentTimeMillis());
lm.setTestProviderLocation(LocationManager.GPS_PROVIDER, loc);
synchronized (this) {
this.wait(2000);
}
Location lastLoc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
System.out.println("Last known longitude: " + Double.toString(lastLoc.getLongitude()) +
"Last known latitude: " + Double.toString(lastLoc.getLatitude()));
assertEquals(35.2276757, locationService.getLongitude());
assertEquals(31.7765488, locationService.getLatitude());
context.unregisterReceiver(locationReceiver);
lm.removeTestProvider(LocationManager.GPS_PROVIDER);
}
private BroadcastReceiver locationReceiver=new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
assertTrue(intent.getAction().equals("android.mtg.custom.intent.action.GPS_LOCATION"));
System.out.println("Action received: " + intent.getAction());
this.notify();
}
};
private IBinder getServiceBinder(){
Intent locationIntent = new Intent(getContext(), LocationService.class);
return bindService(locationIntent);
}
private LocationManager getLocationManager(){
LocationManager lm = (LocationManager)
getContext().getSystemService(Context.LOCATION_SERVICE);
lm.addTestProvider(LocationManager.GPS_PROVIDER,
true, true, true, true, true, true, true,
Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
lm.setTestProviderStatus(LocationManager.GPS_PROVIDER,
LocationProvider.AVAILABLE, null, System.currentTimeMillis());
lm.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);
return lm;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
System.out.println("LocationServiceTest, onLocationChanged, lon:" +
Double.toString(location.getLongitude()) + ", lat:" +
Double.toString(location.getLatitude()));
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
尝试
// Register the listener with the Location Manager to receive location updates
lm.requestLocationUpdates("YOUR PROVIDER", 0, 0, YOUR_LOCATION_LISTENER);