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();
}
}