实现的代码如下:
public class ShowPosition extends Activity implements LocationListener{ TextView lblLatitude;
TextView txtLatitude;
TextView lblLongitude;
TextView txtLongitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showposition);
lblLatitude = (TextView)findViewById(R.id.lblLatitude);
txtLatitude = (TextView)findViewById(R.id.txtLatitude);
lblLongitude = (TextView)findViewById(R.id.lblLongitude);
txtLongitude = (TextView)findViewById(R.id.txtLongitude);
lblLatitude.setText(R.string.latitude);
lblLongitude.setText(R.string.longitude);
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateWithLocation(location);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
} public void updateWithLocation(Location location){
String Latitude;
String Longitude;
if(location != null){
Latitude = "" + location.getLatitude();
Longitude = "" + location.getLongitude();
}
else{
Latitude = "定位失败";
Longitude = "定位失败";
}
txtLatitude.setText(Latitude);
txtLongitude.setText(Longitude);
} @Override
public void onLocationChanged(Location location) {
System.out.println("changed");
updateWithLocation(location);
} @Override
public void onProviderDisabled(String provider) {
updateWithLocation(null);
} @Override
public void onProviderEnabled(String provider) {
} @Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
在2.2的模拟器上实现的时候执行没问题,执行结果会显示在emulator中设置的坐标,但是安装到手机上之后则一直显示检测失败,请问有哪位高手能够解决我的问题?感激不尽啊~
public class ShowPosition extends Activity implements LocationListener{ TextView lblLatitude;
TextView txtLatitude;
TextView lblLongitude;
TextView txtLongitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showposition);
lblLatitude = (TextView)findViewById(R.id.lblLatitude);
txtLatitude = (TextView)findViewById(R.id.txtLatitude);
lblLongitude = (TextView)findViewById(R.id.lblLongitude);
txtLongitude = (TextView)findViewById(R.id.txtLongitude);
lblLatitude.setText(R.string.latitude);
lblLongitude.setText(R.string.longitude);
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateWithLocation(location);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
} public void updateWithLocation(Location location){
String Latitude;
String Longitude;
if(location != null){
Latitude = "" + location.getLatitude();
Longitude = "" + location.getLongitude();
}
else{
Latitude = "定位失败";
Longitude = "定位失败";
}
txtLatitude.setText(Latitude);
txtLongitude.setText(Longitude);
} @Override
public void onLocationChanged(Location location) {
System.out.println("changed");
updateWithLocation(location);
} @Override
public void onProviderDisabled(String provider) {
updateWithLocation(null);
} @Override
public void onProviderEnabled(String provider) {
} @Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
在2.2的模拟器上实现的时候执行没问题,执行结果会显示在emulator中设置的坐标,但是安装到手机上之后则一直显示检测失败,请问有哪位高手能够解决我的问题?感激不尽啊~
解决方案 »
- 短信拦截器 could not open link
- 取得当前显示的Activity
- 我运行模拟器,每隔一段时间总弹出错误提示框怎么解决
- 有没有办法自己写程序让照相机没有预览画面
- content provider异步调服务器接口的问题
- Activity 之间传递的问题!!
- android获取屏幕分辨率
- Android Eclipse中现有项目Ctrl+鼠标左键没办法使用,还有Graphical Layout没办法显示。
- 40岁的老人,android终于入门了。发帖纪念一下。
- Android中使用LitePal,一个表中数据可以正常save,另一个表姐夫怎么也插入不成功,有没有朋友遇到过这个问题啊!
- android短信发送界面 布局的问题求大神解~~~~
- android手机软件开发
而且我这段代码在2.2的模拟器上运行没问题,在2.3的模拟器上运行也无法定位,连onLocationChanged函数都不会触发。
2 如果GPS_PROVIDER(利用卫星)不行,试试network_provider(利用基站)