Android setTestProviderLocation不会在服务测试用例中触发onLocationChanged

Android setTestProviderLocation不会在服务测试用例中触发onLocationChanged,android,android-emulator,gps,Android,Android Emulator,Gps,我有一个实现LocationListener的小型服务。我用我的应用程序手动测试了它,它工作正常。 我还为该服务编写了一个测试用例。 我使用setTestProviderLocation期望服务将收到位置更新。 然而,这并没有发生。 有人知道有什么问题吗?我想强调的是,相同的服务在实际应用中是有效的。 测试用例添加到下面 package com.gkatz.android.mtg.test; import java.util.List; import com.gkatz.android.

我有一个实现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

}
package com.gkatz.android.mtg.test;
导入java.util.List;
导入com.gkatz.android.mtg.LocationService;
导入com.gkatz.android.mtg.LocationService.LocationBinder;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.location.Criteria;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.location.LocationProvider;
导入android.os.Bundle;
导入android.os.IBinder;
导入android.os.SystemClock;
导入android.test.ServiceTestCase;
公共类LocationServiceTest扩展ServiceTestCase实现LocationListener{
公共位置服务测试(){
超级(LocationService.class);
//TODO自动生成的构造函数存根
}
@凌驾
受保护的void setUp()引发异常{
//TODO自动生成的方法存根
super.setUp();
}
公共void testBinding(){
IBinder定位粘合剂;
locationBinder=getServiceBinder();
assertNotNull(locationBinder);
}
公共void testStart(){
Intent locationIntent=newintent(getContext(),LocationService.class);
startService(位置意向);
}
公共无效测试部分(){
LocationService LocationService=getService();
assertNull(locationService);
}
public void testLocationUpdate()引发InterruptedException{
位置粘合剂位置粘合剂;
定位服务定位服务;
Context=getContext();
LocationManager lm=getLocationManager();
上下文。registerReceiver(locationReceiver,
新的IntentFilter(“android.mtg.custom.intent.action.GPS_LOCATION”);
locationBinder=(locationBinder)getServiceBinder();
assertNotNull(locationBinder);
locationService=getService();
assertNotNull(locationService);
位置loc=新位置(LocationManager.GPS\U提供商);
位置:经度(4.890935);
位置:塞特纬度(52.373801);
loc.setTime(System.currentTimeMillis());
lm.SettProviderLocation(LocationManager.GPS_提供商,loc);
系统时钟。睡眠(3000);
位置:经度(35.2276757);
位置:塞特纬度(31.7765488);
loc.setTime(System.currentTimeMillis());
lm.SettProviderLocation(LocationManager.GPS_提供商,loc);
已同步(此){
这是《等待》(2000年);
}
Location lastLoc=lm.getlastnownlocation(LocationManager.GPS\u提供者);
System.out.println(“最后一个已知经度:+Double.toString(lastLoc.getLongitude())+
“最后一个已知纬度:”+Double.toString(lastLoc.getLatitude());
assertEquals(35.2276757,locationService.getLongitude());
assertEquals(31.7765488,locationService.getLatitude());
上下文。未注册接收者(locationReceiver);
lm.removeTestProvider(LocationManager.GPS_PROVIDER);
}
private BroadcastReceiver locationReceiver=新的BroadcastReceiver(){
公共void onReceive(上下文、意图){
assertTrue(intent.getAction().equals(“android.mtg.custom.intent.action.GPS_LOCATION”);
System.out.println(“收到的操作:+intent.getAction());
this.notify();
}
};
私有IBinder getServiceBinder(){
Intent locationIntent=newintent(getContext(),LocationService.class);
返回绑定服务(locationIntent);
}
私有位置管理器getLocationManager(){
LocationManager lm=(LocationManager)
getContext().getSystemService(Context.LOCATION\u服务);
lm.addTestProvider(LocationManager.GPS_PROVIDER,
真的,真的,真的,真的,真的,真的,真的,
标准。功率低,标准。准确度(精细);
lm.SettProviderStatus(LocationManager.GPS_提供程序,
LocationProvider.AVAILABLE,null,System.currentTimeMillis());
lm.SetTestProvidered(LocationManager.GPS_提供程序,true);
lm.RequestLocationUpdate(LocationManager.GPS_提供程序,0,0,此);
返回lm;
}
@凌驾
已更改位置上的公共无效(位置){
//TODO自动生成的方法存根
System.out.println(“LocationServiceTest,onLocationChanged,lon:”+
Double.toString(location.getLongitude())+“,lat:”+
Double.toString(location.getLatitude());
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
//TODO自动生成的方法存根
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
//TODO自动生成的方法存根
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
//TODO自动生成的方法存根
}
}

尝试使用

//向位置管理器注册侦听器以接收位置更新
lm.requestLocationUpdates(“您的提供者”,0,0,您的位置\u侦听器)

在中找到,请尝试

//向位置管理器注册侦听器以接收位置更新
lm.requestLocationUpdates(“您的提供者”,0,0,您的位置\u侦听器)


@gk上找到:实际上我对编写GPS测试用例有相同的要求。你能告诉我是在工作还是我必须做些什么吗。@gk:实际上我对编写GPS测试用例有相同的要求。你能告诉我是在工作还是我必须做些什么吗。