如何在Android中启用Network Provider?

时间:2014-04-26 11:01:16

标签: android android-location

当我使用设备调试我的代码(sony Xperia)以获取使用WIFI的纬度和经度时显示网络提供程序的错误状态.WIFI连接已启用,并且我已将所有必需的权限添加到mainfeast.xml文件。

它向我显示此方法为null。

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);
}
}

1 个答案:

答案 0 :(得分:2)

使用此代码初始化locationmanager

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