gpt4 book ai didi

android - locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) 使用 Wi-Fi 总是返回 false

转载 作者:行者123 更新时间:2023-11-29 20:14:05 26 4
gpt4 key购买 nike

我试过了

NETWORK_PROVIDER 获取地点的经纬度。

我的测试设备是 LG Optimus G Pro(LG-F240L) Android 4.4.2,API 19。

使用的是Wi-Fi网络,不是4G、3G

“isProviderEnabled(LocationManager.NETWORK_PROVIDER)”始终返回 false。

谁能帮帮我?提前致谢!

这是我的代码:

public class GetCurrentLocationActivity extends Service implements LocationListener {
private final Context mContext;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;

protected LocationManager locationManager;

boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean isGetLocation = false;

Location location;
double latitude;
double longitude;

public GetCurrentLocationActivity(Context context){
this.mContext = context;
getLocation();
}



public Location getLocation(){
try{
locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Log.d("test",""+isGPSEnabled);
Log.d("test1",""+isNetworkEnabled);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if(!isGPSEnabled && !isNetworkEnabled){
showSettingsAlert();
} else{
this.isGetLocation = true;

if(isNetworkEnabled){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, (LocationListener) this);

if(locationManager!=null){
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location!=null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if(isGPSEnabled){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
if(locationManager!=null){
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if(location!=null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}else{
showSettingsAlert();
}
}
} catch (Exception e){
e.printStackTrace();
}
return location;
}

public void stopUsingGPS(){
if(locationManager!=null){
locationManager.removeUpdates(GetCurrentLocationActivity.this);
}
}

public double getLatitude(){
if(location!=null){
latitude = location.getLatitude();
}
return latitude;
}

public double getLongitude(){
if(location!=null){
longitude = location.getLongitude();
}
return longitude;
}

public boolean isGetLocation(){
return this.isGetLocation;
}

public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

alertDialog.setTitle("GPS 사용유무셋팅");
alertDialog.setMessage("GPS 셋팅이 되지 않았을수도 있습니다.\n 설정창으로 가시겠습니까?");

// OK 를 누르게 되면 설정창으로 이동합니다.
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);

}
});
// Cancle 하면 종료 합니다.
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

alertDialog.show();
}

@Override
public IBinder onBind(Intent arg0) {
return null;
}

public void onLocationChanged(Location location) {
// TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}
}

这是我的许可:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

最佳答案

代码看起来没问题。在权限方面,您只需要其中的前两个:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

替换条件以检查您的 GPS 是否已启用,如下所示:

            if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, (LocationListener)this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
} //end-fi isGPSEnabled

关于android - locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) 使用 Wi-Fi 总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34307293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com