gpt4 book ai didi

android - 服务测试用例中setTestProviderLocation不触发onLocationChanged

转载 作者:行者123 更新时间:2023-11-29 14:08:01 27 4
gpt4 key购买 nike

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

}

最佳答案

尝试

//向 Location Manager 注册监听器以接收位置更新lm.requestLocationUpdates("YOUR PROVIDER", 0, 0, YOUR_LOCATION_LISTENER);

发现于 Location guide of Android Developers

关于android - 服务测试用例中setTestProviderLocation不触发onLocationChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5581576/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com