当我用设备(sony xperia)调试代码以使用wifi获取经纬度时,显示网络提供商的错误状态。wifi连接已打开,并且所有必需的权限都已添加到mainfast.xml文件中。
这个方法显示为空。

NetworkProvider=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

并在启用网络提供程序时显示位置的空值。
locationmanager.getLastKnownLocation(LocationManager.NETWORK_Provider);

我添加了这些权限。
 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
 <uses-permission android:name="android.permission.ACCESS_LOCATION"/>
 <uses-permission android:name="android.permission.ACCESS_GPS"/>
 <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
 <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
 <uses-permission android:name="android.permission.INSTALL_LOCATION_PROVIDER"/>
 <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
 <uses-permission  android:name="android.permission.CHANGE_WIFI_STATE"/>
 <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>

我怎样才能解决这个问题。
我的代码是
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button) findViewById(R.id.button1);
context=getApplicationContext();
button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v)
    {
    status12=checkStatus();
    if(status12)
    {
        addListenerAction();
    }
    }
});
}

public boolean checkStatus()
{
locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider1=locationManager.getProviders(true);
for(String s : provider1)
{
    if(s.equals(LocationManager.GPS_PROVIDER))
    {
    gpsStatus=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    if(gpsStatus)
    {
        status=true;
    }
    }
    if(s.equals(LocationManager.NETWORK_PROVIDER))
    {
    NetworkProvider=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    if(NetworkProvider)
    {
        status=true;
    }
    }
    if(s.equals(LocationManager.PASSIVE_PROVIDER))
    {
    PassiveProvider=locationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER);

    if(PassiveProvider)
    {
        status=true;
    }
    }
}
return status;
}

private class Abcd implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
    if(loc !=null)
    {
    if(gpsStatus)
    {

        latitude=loc.getLatitude();
        longitude=loc.getLongitude();
        System.out.println("Longitude ="+longitude);
        System.out.println("Latitude ="+latitude);

        try
        {
        geocoder = new Geocoder(context,Locale.getDefault());
        System.out.println("GeoCoder ="+geocoder);
        address=geocoder.getFromLocation(latitude,longitude, 1);
        System.out.println("Address ="+address);
        if(!address.isEmpty())
        {
            Cityname=address.get(0).getLocality();
            country = address.get(0).getCountryName();
            Address=address.get(0).getAddressLine(0);
            System.out.println("Country Name ="+country);
            System.out.println("CityName ="+Cityname);
            System.out.println("Address ="+Address);
            ShowLocation sw=new ShowLocation();

        }
        }
        catch (IOException e) {

        e.printStackTrace();
        }
    }
    if(NetworkProvider)
    {
        latitude=loc.getLatitude();
        longitude=loc.getLongitude();
        System.out.println("Longitude ="+longitude);
        System.out.println("Latitude ="+latitude);

        try
        {
        geocoder = new Geocoder(context,Locale.getDefault());
        System.out.println("GeoCoder ="+geocoder);
        address=geocoder.getFromLocation(latitude,longitude, 1);
        System.out.println("Address ="+address);
        if(!address.isEmpty())
        {
            Cityname=address.get(0).getLocality();
            country = address.get(0).getCountryName();
            Address=address.get(0).getAddressLine(0);
            System.out.println("Country Name ="+country);
            System.out.println("CityName ="+Cityname);
            System.out.println("Address ="+Address);
            ShowLocation sw=new ShowLocation();

        }
        }
        catch (IOException e) {

        e.printStackTrace();
        }
    }
    if(PassiveProvider)
    {
        latitude=loc.getLatitude();
        longitude=loc.getLongitude();
        System.out.println("Longitude ="+longitude);
        System.out.println("Latitude ="+latitude);

        try
        {
        geocoder = new Geocoder(context,Locale.getDefault());
        System.out.println("GeoCoder ="+geocoder);
        address=geocoder.getFromLocation(latitude,longitude, 1);
        System.out.println("Address ="+address);
        if(!address.isEmpty())
        {
            Cityname=address.get(0).getLocality();
            country = address.get(0).getCountryName();
            Address=address.get(0).getAddressLine(0);
            System.out.println("Country Name ="+country);
            System.out.println("CityName ="+Cityname);
            System.out.println("Address ="+Address);
            ShowLocation sw=new ShowLocation();

        }
        }
        catch (IOException e) {

        e.printStackTrace();
        }
    }


    }
}
@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status,
    Bundle extras) {
}
}

public void addListenerAction()
{
locationListener=new Abcd();
if(gpsStatus)
{
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,time,distance, locationListener);
    Criteria criteria=new Criteria();
    provider=locationManager.getBestProvider(criteria, true);
    location=locationManager.getLastKnownLocation(provider);
    System.out.println("Location ="+location);
    locationListener.onLocationChanged(location);
}
if(NetworkProvider)
{
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,time,distance, locationListener);
    Criteria criteria=new Criteria();
    provider=locationManager.getBestProvider(criteria, true);
    location=locationManager.getLastKnownLocation(provider);
    locationListener.onLocationChanged(location);
}
if(PassiveProvider)
{
    locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,time,distance, locationListener);
    Criteria criteria=new Criteria();
    provider=locationManager.getBestProvider(criteria, true);
    location=locationManager.getLastKnownLocation(provider);
    locationListener.onLocationChanged(location);
}
}

最佳答案

使用此代码初始化LocationManager

locationManager = (LocationManager) getSystemService(mContext.LOCATION_SERVICE);

07-24 09:33