这里的东西。我使用了相同的功能,我在分离的类,只包含特定的GPS只是为了尝试。它工作,但我必须说坐标值不正确。现在我试图在我的真实项目中实现相同的功能,但它不工作。它确实检测到我是否启用或禁用了手机中的GPS,但没有出现协调值。android检测GPS提供商,但没有得到的位置
代码,我做了两个类。 Froyo2Activity和MyLocationListener。 Froyo2Activity是仅扩展Activity的主要活动,MyLocationListener实现LocationListener。为了更好地查看,这里是我的代码。
public class Froyo2Activity extends Activity{
Spinner priorSpin, genderSpin;
ImageView femaleView, maleView;
Button submit, agePrev, ageNext, bpPrev, bpNext, pPrev, pNext, butLoc;
SeekBar seekAge, seekBP;
EditText ageEdit, bpEdit, pEdit, locEdit, fname, lname;
TextView tv;
String commandline="", ageString="", bpString="", pString="";
int age = 0, bp = 0, p = 0;
double latitude, longitude, altitude, pitch = 0;
private static final int REQUEST_CODE = 10;
private LocationManager locationManager;
private String provider;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_froyo2);
fname = (EditText) findViewById(R.id.editText1);
lname = (EditText) findViewById(R.id.editText2);
ageEdit = (EditText) findViewById(R.id.editText7);
pEdit = (EditText) findViewById(R.id.editText9);
locEdit = (EditText) findViewById(R.id.editText3);
femaleView = (ImageView)findViewById(R.id.femaleView);
maleView = (ImageView)findViewById(R.id.maleView);
priorSpin = (Spinner) findViewById(R.id.prior_spin);
genderSpin = (Spinner)findViewById(R.id.gender_spin);
tv = (TextView) findViewById(R.id.textView14);
//---set the GPS---
//Get the location manager
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
if(MyLocationListener.latitude>0)
{
latitude=MyLocationListener.latitude;
longitude=MyLocationListener.longitude;
tv.setText("Latitude: "+ Double.toString(latitude) +" Longitude: "+ Double.toString(longitude)+" Altitude:"+ Double.toString(pitch)+"");
}
else
{
tv.setText("Signal Not Available");
}
} else {
tv.setText("GPS is not turned on...");
}
//---end GPS---
这是MyLocationListener
代码package com.example.froyo2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.TextView;
import android.widget.Toast;
public class MyLocationListener implements LocationListener {
public static double latitude;
public static double longitude;
public static double salah=90;
@Override
public void onLocationChanged(Location loc)
{
// loc.getLatitude();
// loc.getLongitude();
latitude=loc.getLatitude();
longitude=loc.getLongitude();
salah=1;
}
@Override
public void onProviderDisabled(String provider)
{
salah=2;
}
@Override
public void onProviderEnabled(String provider)
{
salah=3;
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
salah=4;
}
}
没有人知道如何解决这个问题。就像我之前说过的那样,当我用自己的课堂尝试gps功能时;意思是没有执行其他功能或活动,特别是仅针对GPS功能测试的类,它确实有效 - 至少指出位置的编号。但是,在实现具有其他功能的GPS时,它表示信号不可用(请参阅代码)。如果这个问题能够解决,这将是有益的。
感谢您的提前。 (SaraBrown)
如果你解释为什么有时GPS提供商不能为你提供一个位置,为什么/何时使用网络提供商作为替代方案 – paulkayuk
我试着将GPS_PROVIDER更改为NETWORK_PROVIDER,但它'不幸关闭'。 –