我有四个非常小的类。以下是四个班级。移动视图不会显示除“位置不可用”之外的任何内容。并且记录进入“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;
}
}
你添加权限<使用许可权的android:NAME = “android.permission.ACCESS_COARSE_LOCATION”>使用许可权> \t <使用许可权的android:NAME =“机器人。 permission.ACCESS_FINE_LOCATION“> uses-permission> – 2012-03-12 09:26:06
我只添加了”FINE_LOCATION“。但是现在我又添加了另一个。我仍然没有看到任何东西。当我在android中查看地图时,它显示我的位置达到某个级别。 – dinesh707 2012-03-12 09:32:46
你有权限吗? GPS定位可能需要一些时间,因此您可能只需稍等一下。试着把它放在室外,至少不要放在沙坑里。 – 2012-03-12 09:33:34