为什么GPS位置不起作用?

时间:2012-03-12 09:24:20

标签: android gps

我有四个非常小的课程。以下是四个班级。移动视图不会向我显示“位置不可用”以外的任何内容。并且日志记录上升到“Location Finder Started”。没有别的动机。

主要活动

public class CarbonTrackerMainActivity extends Activity {
     private MainView mainView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mainView = new MainView(this);
    locationFinder();
    setContentView(mainView);
}

private void locationFinder(){
    Log.d("Car","Location Finder Started !!");
    LocationManager mlocManager = (LocationManager)getSystemService(this.LOCATION_SERVICE);
    LocationListener mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

    }
}

申请的视图

public class MainView extends View{
    public MainView(Context context) {
        super(context);
    }

     protected void onDraw(Canvas canvas){
         Paint background = new Paint();
         background.setColor(Color.GRAY);
         canvas.drawRect(0, 0, getWidth(), getHeight(), background);
         Paint text = new Paint();
         text.setColor(Color.BLACK);
         text.setTextSize(15);
         canvas.drawText(DataHolder.getDataHolderObject().getLocationInTxt(), 10, 10, text);
         invalidate();
     }
}

位置监听器

public class MyLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location loc){
    loc.getLatitude();
    loc.getLongitude();
    DataHolder.getDataHolderObject().setLocation(loc);
    Log.d("Car","Found one location!");
}

@Override
public void onProviderDisabled(String provider){
    Log.d("Carbon","GPS Disabled!");
}

@Override
public void onProviderEnabled(String provider){
    Log.d("Carbon","GPS Enabled!");
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras){
    Log.d("Carbon","GPS Status Changed!");
}
}

单例传递变量

public class DataHolder {

    private static DataHolder dataHolderObject;
    public Location location = null;

    //======== Singleton Code =====================
    private DataHolder(){}
    public static DataHolder getDataHolderObject(){
        if (dataHolderObject == null)
            dataHolderObject = new DataHolder();
        return dataHolderObject;
    }
    //======== End of singleton ===================

    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

    public String getLocationInTxt() {
        String text;
        if (location==null)
            return "Location Unavailable";
        text = "My current location is:\n" + "Latitud = " + location.getLatitude() +"\nLongitud = " + location.getLongitude();
        return text;
    }
}

4 个答案:

答案 0 :(得分:1)

你在位置上犯了错误。检查您的位置是否为空?

public void setCriteria() {
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
        provider = locationManager.getBestProvider(criteria, true);
        if (provider == null) {
            provider = LocationManager.GPS_PROVIDER;
        }
    }

//设置标准并检查您的位置是否为空

    if (currentLocation == null) {
        currentLocation = new Location(provider);
    }

这将解决您的问题。

答案 1 :(得分:0)

在某些设备中打印DataHolder.getDataHolderObject()。getLocationInTxt()之前,我也猜测gps没有足够的时间锁定某个位置(在开始侦听位置更改后立即绘制视图)。

您可以使用Timer()绘制主视图x秒,这在位于“public void onLocationChanged”中更新位置时效率有点甚至更高,更改/重绘主视图以便绘制新位置。

答案 2 :(得分:0)

我只是让手机靠近窗户5分钟。然后它开始工作。所以我发布的代码没有任何问题。但仍然考虑关于seeting标准的答案

答案 3 :(得分:-1)

尝试在开放空间中移动手机约50 - 100米..它开始提供数据......