本文介绍了GPS不能获取用户位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图获取当前用户的位置(经度和纬度),但 onLocationChanged
方法不获取调用
但其他的方法,如公共无效onProviderDisabled(字符串提供商)。请帮帮忙,有什么可问题呢?
我有一个类:
包com.java codegeeks.android.lbs;进口android.app.Activity;
进口android.os.Bundle; 进口android.app.Activity; 进口android.content.Context; 进口android.location.Location; 进口android.location.LocationListener; 进口android.location.LocationManager; 进口android.os.Bundle; 进口android.view.View; 进口android.view.View.OnClickListener; 进口android.widget.Button; 进口android.widget.Toast; 公共类LbsGeocodingActivity延伸活动{ 私有静态最后长MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; //在米 私有静态最后长MINIMUM_TIME_BETWEEN_UPDATES = 1000; //(毫秒) 受保护的LocationManager的LocationManager; 保护按钮retrieveLocationButton; @覆盖 公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 的setContentView(R.layout.main); retrieveLocationButton =(按钮)findViewById(R.id.retrieve_location_button); 的LocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, 新MyLocationListener() ); retrieveLocationButton.setOnClickListener(新OnClickListener(){ @覆盖 公共无效的onClick(视图v){ showCurrentLocation(); } }); } 保护无效showCurrentLocation(){ 地点= locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 如果(位置!= NULL){ 字符串消息=的String.format( 当前位置\\ n经度:%1 $ S \\北纬:%2 $ S location.getLongitude(),location.getLatitude() ); Toast.makeText(LbsGeocodingActivity.this,消息, Toast.LENGTH_LONG).show(); } } 私有类MyLocationListener实现LocationListener的{ 公共无效onLocationChanged(地点){ 字符串消息=的String.format( 新位置\\ n经度:%1 $ S \\北纬:%2 $ S location.getLongitude(),location.getLatitude() ); Toast.makeText(LbsGeocodingActivity.this,消息,Toast.LENGTH_LONG).show(); } 公共无效onStatusChanged(字符串s INT I,叠B){ Toast.makeText(LbsGeocodingActivity.this,供应商的地位变化, Toast.LENGTH_LONG).show(); } 公共无效onProviderDisabled(String s)将{
Toast.makeText(LbsGeocodingActivity.this, 用户。GPS禁用提供商关闭, Toast.LENGTH_LONG).show(); } 公共无效onProviderEnabled(String s)将{ Toast.makeText(LbsGeocodingActivity.this, 由用户。启用GPS提供商接通, Toast.LENGTH_LONG).show(); }
}
}
解决方案
只是扩展了GpsStatus.Listener您的活动。
像如下:
ublic类MyActivity实现LocationListener的,
GpsStatus.Listener
和检查您的权限:
I am trying to get the current user location (latitude and longitude ) but onLocationChanged
method is not getting calledbut other methods like public void onProviderDisabled(String provider). Please help, what can be the problem ?I have a class:
package com.javacodegeeks.android.lbs;
import android.app.Activity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class LbsGeocodingActivity extends Activity {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
protected LocationManager locationManager;
protected Button retrieveLocationButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
retrieveLocationButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showCurrentLocation();
}
});
}
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(LbsGeocodingActivity.this, message,
Toast.LENGTH_LONG).show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(LbsGeocodingActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(LbsGeocodingActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
}
解决方案
Just extends the GpsStatus.Listener in your activity.like as following:ublic class MyActivity implements LocationListener, GpsStatus.Listener
and check your permission:
这篇关于GPS不能获取用户位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!