实现的代码如下:
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中设置的坐标,但是安装到手机上之后则一直显示检测失败,请问有哪位高手能够解决我的问题?感激不尽啊~
解决方案 »
- 如何把SDcard的文件引入R文件,加以引用?新手求指教!!!
- android:layout_marginBottom使用方法
- ExpandableListView 子元素按下 的动作怎么捕捉
- 如何实现menu底部菜单的四个菜单项MenuItem在一行显示?
- 横竖屏颠倒后,什么时候取屏幕的宽高比较合适?
- 急求 Android 百度地图 绘制多条折线方法
- android 的官方API应该怎么看啊
- 关于ITelephony中getCellLocation返回结果转化的问题
- 为什么下载了Android SDK Manager,但是无法下载具体的SDK呢?
- MediaCodec+surfacetexture 播放视频 中途替换surface
- android短信发送界面 布局的问题求大神解~~~~
- android手机软件开发
而且我这段代码在2.2的模拟器上运行没问题,在2.3的模拟器上运行也无法定位,连onLocationChanged函数都不会触发。
2 如果GPS_PROVIDER(利用卫星)不行,试试network_provider(利用基站)