0
任何人都可以指導如何檢查與條件的互聯網連接設置?我需要類似於這組代碼的東西。ACTION_WIRELESS_SETTINGS連接設置
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
下面是我的代碼,我想用互聯網連接檢查代替GPS檢查。
public class MainActivity extends FragmentActivity implements LocationListener, LocationSource{
private GoogleMap map;
private OnLocationChangedListener mListener;
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if(locationManager != null)
{
boolean gpsIsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean networkIsEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(gpsIsEnabled)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000L, 10F, this);
else if(networkIsEnabled)
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000L, 10F, this);
}
setUpMapIfNeeded();
}
//after user install/update Google Play Service, user might return to this activity
//to stop or resume the activity, onPause and onResume is needed
@Override
public void onPause()
{
if(locationManager != null)
{
locationManager.removeUpdates(this);
}
super.onPause();
}
@Override
public void onResume()
{
super.onResume();
setUpMapIfNeeded();
if(locationManager != null)
{
map.setMyLocationEnabled(true); //detect current location
}
}