2013-07-11 3 views
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 
     } 
    } 

ответ

0

вы можете использовать что-то вроде этого ..

public boolean isOnline(final Context context) { 
ConnectivityManager cm = (ConnectivityManager)   context.getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
Builder dialogb = new AlertDialog.Builder(context); 
if (netInfo != null && netInfo.isConnected()) { 
    return true; 
} 
     dialogb.setTitle("No Internet.. :("); 
     dialogb.setMessage("We need internet to work. Kindly switch it on."); 
     dialogb.setPositiveButton("Turn on", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
       // TODO Auto-generated method stub 
       Intent myIntent = new Intent(Settings.ACTION_WIRELESS_SETTINGS); 
       context.startActivity(myIntent); 
       //get gps 

      } 
     }); 
     dialogb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
       // TODO Auto-generated method stub 


      } 
     }); 
     dialogb.show(); 
return false; 
}