package
com.example.currentloactioninfragment;
import
android.Manifest;
import
android.annotation.SuppressLint;
import
android.content.Context;
import
android.content.Intent;
import
android.content.pm.PackageManager;
import
android.location.Location;
import
android.location.LocationManager;
import
android.os.Bundle;
import
android.os.Looper;
import
android.provider.Settings;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.Button;
import
android.widget.TextView;
import
android.widget.Toast;
import
androidx.annotation.NonNull;
import
androidx.core.content.ContextCompat;
import
androidx.core.content.PackageManagerCompat;
import
androidx.fragment.app.Fragment;
import
com.google.android.gms.location.FusedLocationProviderClient;
import
com.google.android.gms.location.LocationCallback;
import
com.google.android.gms.location.LocationRequest;
import
com.google.android.gms.location.LocationResult;
import
com.google.android.gms.location.LocationServices;
import
com.google.android.gms.tasks.OnCompleteListener;
import
com.google.android.gms.tasks.Task;
public
class
MainFragment
extends
Fragment {
Button btLocation;
TextView tvLatitude, tvLongitude;
FusedLocationProviderClient client;
@Override
public
View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_main,
container,
false
);
btLocation = view.findViewById(R.id.bt_location);
tvLatitude = view.findViewById(R.id.tv_latitude);
tvLongitude = view.findViewById(R.id.tv_longitude);
client = LocationServices
.getFusedLocationProviderClient(
getActivity());
btLocation.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View view)
{
if
(ContextCompat.checkSelfPermission(
getActivity(),
Manifest.permission
.ACCESS_FINE_LOCATION)
== PackageManager
.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(
getActivity(),
Manifest.permission
.ACCESS_COARSE_LOCATION)
== PackageManager
.PERMISSION_GRANTED) {
getCurrentLocation();
}
else
{
requestPermissions(
new
String[] {
Manifest.permission
.ACCESS_FINE_LOCATION,
Manifest.permission
.ACCESS_COARSE_LOCATION },
100
);
}
}
});
return
view;
}
@Override
public
void
onRequestPermissionsResult(
int
requestCode,
@NonNull
String[] permissions,
@NonNull
int
[] grantResults)
{
super
.onRequestPermissionsResult(
requestCode, permissions, grantResults);
if
(requestCode ==
100
&& (grantResults.length >
0
)
&& (grantResults[
0
] + grantResults[
1
]
== PackageManager.PERMISSION_GRANTED)) {
getCurrentLocation();
}
else
{
Toast
.makeText(getActivity(),
"Permission denied"
,
Toast.LENGTH_SHORT)
.show();
}
}
@SuppressLint
(
"MissingPermission"
)
private
void
getCurrentLocation()
{
LocationManager locationManager
= (LocationManager)getActivity()
.getSystemService(
Context.LOCATION_SERVICE);
if
(locationManager.isProviderEnabled(
LocationManager.GPS_PROVIDER)
|| locationManager.isProviderEnabled(
LocationManager.NETWORK_PROVIDER)) {
client.getLastLocation().addOnCompleteListener(
new
OnCompleteListener<Location>() {
@Override
public
void
onComplete(
@NonNull
Task<Location> task)
{
Location location
= task.getResult();
if
(location !=
null
) {
tvLatitude.setText(
String.valueOf(
location
.getLatitude()));
tvLongitude.setText(
String.valueOf(
location
.getLongitude()));
}
else
{
LocationRequest locationRequest
=
new
LocationRequest()
.setPriority(
LocationRequest
.PRIORITY_HIGH_ACCURACY)
.setInterval(
10000
)
.setFastestInterval(
1000
)
.setNumUpdates(
1
);
LocationCallback
locationCallback
=
new
LocationCallback() {
@Override
public
void
onLocationResult(
LocationResult
locationResult)
{
Location location1
= locationResult
.getLastLocation();
tvLatitude.setText(
String.valueOf(
location1
.getLatitude()));
tvLongitude.setText(
String.valueOf(
location1
.getLongitude()));
}
};
client.requestLocationUpdates(
locationRequest,
locationCallback,
Looper.myLooper());
}
}
});
}
else
{
startActivity(
new
Intent(
Settings
.ACTION_LOCATION_SOURCE_SETTINGS)
.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK));
}
}
}