-2

I am New To Android and I am Trying To Find My Location in My App Before Going to Check My Location I want to Check My Gps and Internet Connection

ie: If Gps is Not Enable By calling Intent We are Call Settings and WE Enable it Likewise if Internet Mobile Data is Not Enable how can we Enable Mobile Data Through Intent

For Example If we want to Open Gps Through Intent We call Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);

lisewise for Enable Mobile Data What Intent We call?

Here is My Code:

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

        private GoogleMap mMap;
        LocationManager locationManager;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);

            checkGPSStatus();
                SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map);
                mapFragment.getMapAsync(this);

        }



     public void onSearch(View view)
        {
            EditText location_tf = (EditText)findViewById(R.id.search);
            String location = location_tf.getText().toString();
            List<Address> addressList = null;
            if(location != null || !location.equals(""))
            {
                Geocoder geocoder = new Geocoder(this);
                try {
                    addressList = geocoder.getFromLocationName(location , 1);


                } catch (IOException e) {
                    e.printStackTrace();
                }

                Address address = addressList.get(0);
                LatLng latLng = new LatLng(address.getLatitude() , address.getLongitude());
                mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
                mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));

            }
        }

@Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
      //  LatLng sydney = new LatLng(0, 0);
        mMap.addMarker(new MarkerOptions().position(new LatLng(0,0)).title("Marker"));

    }
    private void checkGPSStatus() {
        LocationManager locationManager = null;
        boolean gps_enabled = false;
        boolean network_enabled = false;
        if ( locationManager == null) {
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        }
        try {
            gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex){}
        try {
            network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex){}
        if ( !gps_enabled && !network_enabled ){
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setMessage("GPS not enabled");
            dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //this will navigate user to the device location settings screen
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(intent);
                }
            });
            AlertDialog alert = dialog.create();
            alert.show();
        }
    }
  • Possible duplicate of [How to check internet access on Android? InetAddress never timeouts...](http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts) – 2Dee Dec 18 '15 at 12:30
  • Possible duplicate of [Detect whether there is an Internet connection available on Android](http://stackoverflow.com/questions/4238921/detect-whether-there-is-an-internet-connection-available-on-android) – Nabeel K Dec 18 '15 at 12:31
  • If Gps is Not Enable By calling Intent We are Call Settings and WE Enable it Likewise if Internet Mobile Data is Not Enable how can we Enable Mobile Data Through Intent @2Dee – SabarishKarthick Dec 18 '15 at 12:36

1 Answers1

0
With The Following permissions:

        <!-- Internet Permissions -->
        <uses-permission android:name="android.permission.INTERNET" />

        <!-- Network State Permissions -->
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Try This

    public boolean isConnectingToInternet(){
            ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
              if (connectivity != null) 
              {
                  NetworkInfo[] info = connectivity.getAllNetworkInfo();
                  if (info != null) 
                      for (int i = 0; i < info.length; i++) 
                          if (info[i].getState() == NetworkInfo.State.CONNECTED)
                          {
                              return true;
                          }

              }
              return false;
        }

Use This as follow:

    if (isConnectingToInternet()) {
                        // Internet Connection is Present
                        // make HTTP requests
                        showAlertDialog(AndroidDetectInternetConnectionActivity.this, "Internet Connection",
                                "You have internet connection", true);
                    } else {
                        // Internet connection is not present
                        // Ask user to connect to Internet
                         AlertDialog alertDialog = new AlertDialog.Builder(context).create();

                        // Setting Dialog Title
                        alertDialog.setTitle("Internet Connection");

                       // Setting Dialog Message
                       alertDialog.setMessage("You don't have internet connection.");

                      // Setting alert dialog icon
                      alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

                      // Setting OK Button
                      alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int which) {
                      Intent intent = new Intent(Intent.ACTION_MAIN);
                      intent.setClassName("com.android.phone",                       "com.android.phone.NetworkSetting");
                      startActivity(intent);
                      }
                     });

                     // Showing Alert Message
                     alertDialog.show();
        }
Amy
  • 4,034
  • 1
  • 20
  • 34
  • Write this function outside of the onCreate(). – Amy Dec 18 '15 at 12:21
  • If Gps is Not Enable By calling Intent We are Call Settings and WE Enable it Likewise if Internet Mobile Data is Not Enable how can we Enable Mobile Data Through Intent For Example If we want to Open Gps Through Intent We call Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); lisewise for Enable Mobile Data What Intent We call? @Amy – SabarishKarthick Dec 18 '15 at 12:47