2012-03-12 180 views
0

我有四个非常小的类。以下是四个班级。移动视图不会显示除“位置不可用”之外的任何内容。并且记录进入“Location Finder Started”。没有别的触发器。为什么GPS定位不起作用?

主要Actility

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; 
    } 
} 
+1

你添加权限<使用许可权的android:NAME = “android.permission.ACCESS_COARSE_LOCATION”> \t <使用许可权的android:NAME =“机器人。 permission.ACCESS_FINE_LOCATION“> 2012-03-12 09:26:06

+0

我只添加了”FINE_LOCATION“。但是现在我又添加了另一个。我仍然没有看到任何东西。当我在android中查看地图时,它显示我的位置达到某个级别。 – dinesh707 2012-03-12 09:32:46

+0

你有权限吗? GPS定位可能需要一些时间,因此您可能只需稍等一下。试着把它放在室外,至少不要放在沙坑里。 – 2012-03-12 09:33:34

回答

0

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

-1

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

+2

由于位置为空,所以发生错误。 – Scorpion 2012-03-12 09:37:07

+0

天蝎座是绝对正确的... – 2012-03-12 09:47:15

+0

如果它为空,请尝试处理它不是空的情况下,并忽略空例.. – Vinay 2012-03-12 09:47:26

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); 
    } 

这将解决您的问题。

+0

这有什么用?他已经将提供商设置为GPS_PROVIDER ...并且他清楚他想使用GPS而不是Wifi ......您只是重定向并设置提供商......这是一种冗余方式 – Vinay 2012-03-12 09:53:31

+0

问题是听众不要触发任何方法。那么我如何检查位置为空? – dinesh707 2012-03-12 10:05:11

+0

Thankx代码。将手机靠近窗户5分钟后开始工作。我会为你的代码提供赞成票。 – dinesh707 2012-03-12 10:26:50

0

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

你可以用一个Timer()函数绘制你的主视图,这个效果有点低效,甚至更好,当位置在“public void onLocationChanged”中更新/重画主视图时,它会绘制新的位置。