【发布时间】:2013-12-16 18:14:52
【问题描述】:
我也尝试通过 DDMS 手动和 GPX 传递 GPS 坐标。 @我的结局没有任何效果。始终显示“GPS 可用性差”。从调试中我看到 GPS 已启用。 谢谢。
public class SurroundgpsActivity extends Activity implements LocationListener {
/** Called when the activity is first created. */
//For GPS
Timer timer = null;
TimerTask scanTask = null;
private LocationManager locationManager;
private Location location;
private Button BtnGpsCoord=null;
private String gpsFolder = "/sdcard/assignment1/gps1/";
//MapView mapView = (MapView) findViewById(R.id.mapview);
//mapView.setBuiltInZoomControls(true);
protected boolean isRouteDisplayed() {
return false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
BtnGpsCoord=(Button) findViewById(R.id.button1);
BtnGpsCoord.setOnClickListener(new View.OnClickListener() {
public void onClick(View V) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Getting GPS...", Toast.LENGTH_LONG).show();
final Handler handler=new Handler();
timer =new Timer();
scanTask = new TimerTask(){
public void run()
{
handler.post(new Runnable()
{
public void run()
{
getGpsAutomatically();
System.out.println("Calling getGpsAutomatically");
}
});
}
};
// setting the timer of 10 minutes
timer.schedule(scanTask, 300, 600000);
}
});
}
private void getGpsAutomatically()
{
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
System.out.println("\nisGpsEnabled:"+isGpsEnabled);
if (isGpsEnabled == false)
{
String err_msg = "GPS is disabled, Kindly enable it.";
showGPSError(err_msg);
}
else
{
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
//String provider = locationManager.getBestProvider(criteria, true);
String provider = LocationManager.GPS_PROVIDER;
locationManager.requestLocationUpdates(provider, 1000L, 500.0f, this);
location = locationManager.getLastKnownLocation(provider);
System.out.println("\nLocation:"+location);
try
{
printGpsLocation(location);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void printGpsLocation(Location location) throws IOException
{
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
if (location != null)
{
double lat = location.getLatitude();
double lng = location.getLongitude();
String destination ="latitude = "+lat+";longitude = "+lng;
fOut = openFileOutput(gpsFolder + "location.txt", Context.MODE_WORLD_WRITEABLE);
osw = new OutputStreamWriter(fOut);
osw.write(destination);
osw.flush();
osw.close();
fOut.close();
locationManager.removeUpdates(this);
location.reset();
}
else
{
String err_msg = "GPS is working poorly.";
showGPSError(err_msg);
}
}
private void showGPSError(String errorMsg)
{
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage(errorMsg);
alertDialog.setButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
return;
}
});
alertDialog.show();
}
@Override
public void onLocationChanged(Location location)
{
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(criteria, true);
location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 1000L, 500.0f, this);
}
@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
}
@Override
public void onDestroy()
{
super.onDestroy();
}
}
【问题讨论】:
-
您是否在清单中设置了 ACCESS_MOCK_LOCATION 权限(以及 ACCESS_FINE_LOCATION 等其他权限)。
-
尝试了解 GPS 是用于设备定位的,它不能在模拟器中工作
-
是的,模拟器可以模拟 GPS。但是您必须通过 adb(或使用 Eclipse)传递位置(坐标)。要让应用程序接受这些坐标,应用程序需要 ACCESS_MOCK_LOCATION 权限。
-
嗨斯特凡,谢谢。我添加了 ACCESS_MOCK_LOCATION 但没有用。