2014-04-26 35 views
3

當我調試我的設備(索尼XPERIA)代碼使用網絡provider.WIFI連接WIFI顯示虛假的地位越來越緯度和經度上,我添加了所有必需的權限mainfeast.xml文件。如何在android中啓用網絡提供程序?

它告訴我空此方法。

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

我得到價值形態全球定位系統供應商,而是想通過被動或網絡供應商的訪問。 – user3509581

+0

你初始化了locationmanager嗎? –

+0

雅我用setSystemServices(Context.LOCATION_SERVICE)方法初始化 – user3509581

回答

2

使用此代碼初始化的LocationManager

locationManager = (LocationManager) getSystemService(mContext.LOCATION_SERVICE); 
相關問題