Getting current location GPS

Referred from Swiftguy Answer: https://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android.

MainActivity:

public class MainActivity extends AppCompatActivity {

private String TAG = "MainActivity";

@BindView(R.id.main_location_tv)

TextView mTVLocation;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ButterKnife.bind(this);

LocationManager locationManager = (LocationManager)

getSystemService(Context.LOCATION_SERVICE);

LocationListener locationListener = new MyLocationListener();

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.

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION,

Manifest.permission.ACCESS_COARSE_LOCATION},101);

return;

}

locationManager.requestLocationUpdates(

LocationManager.GPS_PROVIDER, 1000, 10, locationListener);

new MyLocationListener().onLocationChanged(new Location("")); // here method called explicitly to get first time get LatLang

}

private class MyLocationListener implements LocationListener {

@Override

public void onLocationChanged(Location loc) {

mTVLocation.setText("");

String longitude = "Longitude: " + loc.getLongitude();

Log.v(TAG, longitude);

String latitude = "Latitude: " + loc.getLatitude();

Log.v(TAG, latitude);

String s = longitude + "\n" + latitude ;

mTVLocation.setText(s);

}

@Override

public void onProviderDisabled(String provider) {}

@Override

public void onProviderEnabled(String provider) {}

@Override

public void onStatusChanged(String provider, int status, Bundle extras) {}

}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.yourcompany.currentlocationproject">

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

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme">

<activity android:name=".MainActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:id="@+id/main_location_tv"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

</LinearLayout>