如何在 android 中启用 Network Provider?
Posted
技术标签:
【中文标题】如何在 android 中启用 Network Provider?【英文标题】:How enabled Network Provider in android? 【发布时间】:2014-06-12 03:50:20 【问题描述】:当我使用设备(索尼 Xperia)调试我的代码以使用 WIFI 获取纬度和经度时,显示网络提供商的错误状态。WIFI 连接已打开并且我将所有必需的权限添加到 mainfeast.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);
【问题讨论】:
我从 Gps 提供商那里获得价值,但希望通过被动或网络提供商访问。 你初始化locationmanager了吗? 我用 setSystemServices(Context.LOCATION_SERVICE) 方法初始化了 当我使用 Gps 运行我的代码时,它会为我提供经度和纬度,但是当我打开设备的 wifi 时,它只会为我提供被动提供者 和它不允许定位的被动提供者 【参考方案1】:使用此代码初始化 locationmanager
locationManager = (LocationManager) getSystemService(mContext.LOCATION_SERVICE);
【讨论】:
以上是关于如何在 android 中启用 Network Provider?的主要内容,如果未能解决你的问题,请参考以下文章
LocationManager 他的 Network_Provider 不会启用
没有 TLS 的 Android P:network-security-config: cleartextTrafficPermitted IP 不可能(仅域)