You need to use permission first:
Copy<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Next you inside onCreate() method use below code to get access to GPS.
Copy@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
2000, 1, this);
}
Now if GPS is not enabled then you need to refer below code. For detailed explanation, you can refer how to get[DEAD LINK] [current location in android]1 tutorial.
CopyIntent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
Here we have used intents to redirect user to location settings page so that he can enable the GPS under location settings.
Answer from amit duhan on Stack OverflowVideos
You need to use permission first:
Copy<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Next you inside onCreate() method use below code to get access to GPS.
Copy@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
2000, 1, this);
}
Now if GPS is not enabled then you need to refer below code. For detailed explanation, you can refer how to get[DEAD LINK] [current location in android]1 tutorial.
CopyIntent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
Here we have used intents to redirect user to location settings page so that he can enable the GPS under location settings.
you can achieve this easily. you have to write code in main activity. and Main activity implements Locationlistner interface. and overload its four methods. here i'am retrieving my Longitude and Latitide co-ordinates in a textview when application started. and you have to give permission for access location in your manifest file.
as under my code.
Copypackage com.nisarg.gpsdemo;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements LocationListener {
private LocationManager locationManager;
private TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview=(TextView)findViewById(R.id.textView);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
onLocationChanged(location);
}
@Override
public void onLocationChanged(Location location) {
double longitude=location.getLongitude();
double latitude=location.getLatitude();
textview.setText("Longitude: "+longitude+" Latitide: "+latitude);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
and you have to give permission like this in manifest file.
Copy <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
And last warning. you have to give permission to your application and start mobile data and GPS before you open application. either it will be crashed.
that's it. hope it will help you. :)