0% found this document useful (0 votes)
78 views

Program To Locate User's Current Location:-Activity - Main - XML: - Main - Axtivity - XML

This document contains code for an Android program that locates the user's current location and displays the address. It uses the FusedLocationProviderClient to get location updates and a Geocoder to get the address from the coordinates. Location permission is requested and on receiving the location, an intent service is started to lookup the address and send the result back to the activity to display.

Uploaded by

Monika Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Program To Locate User's Current Location:-Activity - Main - XML: - Main - Axtivity - XML

This document contains code for an Android program that locates the user's current location and displays the address. It uses the FusedLocationProviderClient to get location updates and a Geocoder to get the address from the coordinates. Location permission is requested and on receiving the location, an intent service is started to lookup the address and send the result back to the activity to display.

Uploaded by

Monika Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Program to locate user's current location:- main_axtivity.

xml:-

activity_main.xml:- import androidx.annotation.NonNull;

<?xml version=”1.0” encoding=”utf-8”?> import androidx.appcompat.app.AppCompatActivity;

<RelativeLayout import androidx.core.app.ActivityCompat;


xmlns:android=http://schemas.android.com/apk/res/android
import androidx.core.content.ContextCompat;
xmlns:tools=http://schemas.android.com/tools
import android.Manifest;
android:orientation=”vertical”
import android.content.Intent;
android:layout_width=”match_parent”
import android.content.pm.PackageManager;
android:layout_height=”match_parent”
import android.location.Geocoder;
tools:context=”.MainActivity”>
import android.location.Location;
<TextView
import android.os.Bundle;
android:layout_marginTop=”20dp”
import android.os.Handler;
android:layout_width=”fill_parent”
import android.os.ResultReceiver;
android:layout_height=”wrap_content”
import android.util.Log;
android:text=”Get Current Location and City Name”
import android.widget.TextView;
android:textAlignment=”center”
import android.widget.Toast;
android:layout_centerHorizontal=”true”
import
android:textSize=”20sp” /> com.google.android.gms.location.FusedLocationProviderClie
nt;
<TextView
import com.google.android.gms.location.LocationCallback;
android:layout_width=”wrap_content”
import com.google.android.gms.location.LocationRequest;
android:layout_height=”wrap_content”
import com.google.android.gms.location.LocationResult;
android:id=”@+id/textView”
import com.google.android.gms.location.LocationServices;
android:layout_centerInParent=”true”
public class MainActivity extends AppCompatActivity {
android:textSize=”16sp”
private FusedLocationProviderClient fusedLocationClient;
android:textStyle=”bold”/>
private static final int
</RelativeLayout>
LOCATION_PERMISSION_REQUEST_CODE = 2;

private LocationAddressResultReceiver
addressResultReceiver;

private TextView currentAddTv;

private Location currentLocation;

private LocationCallback locationCallback;

@Override
protected void onCreate(Bundle savedInstanceState) { fusedLocationClient.requestLocationUpdates(locationReq
uest, locationCallback, null);
super.onCreate(savedInstanceState);
}
setContentView(R.layout.activity_main);
}
addressResultReceiver = new
locationAddressResultReceiver(new Handler()); @SuppressWarnings(“MissingPermission”)

currentAddTv = findViewById(R.id.textView); private void getAddress() {

fusedLocationClient = if (!Geocoder.isPresent()) {
locationServices.getFusedLocationProviderClient(this);
toast.makeText(MainActivity.this, “Can’t find current
locationCallback = new LocationCallback() { address, “,

@Override toast.LENGTH_SHORT).show();

public void onLocationResult(LocationResult locationResult) { return;

currentLocation = locationResult.getLocations().get(0); }

getAddress(); intent intent = new Intent(this,


getAddressIntentService.class);
}
intent.putExtra(“add_receiver”,
};
addressResultReceiver);
startLocationUpdates();
intent.putExtra(“add_location”, currentLocation);
}
startService(intent);
@SuppressWarnings(“MissingPermission”)
}
private void startLocationUpdates() {
@Override
if (ContextCompat.checkSelfPermission(this,
public void onRequestPermissionsResult(int
manifest.permission.ACCESS_FINE_LOCATION) !=
requestCode, @NonNull String[] permissions, @NonNull
packageManager.PERMISSION_GRANTED) {
int[] grantResults) {
activityCompat.requestPermissions(this, new
if (requestCode ==
string[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE) {

LOCATION_PERMISSION_REQUEST_CODE); if (grantResults.length > 0 && grantResults[0] ==


packageManager.PERMISSION_GRANTED) {
}
startLocationUpdates();
else {
}
locationRequest locationRequest = new LocationRequest();
else {
locationRequest.setInterval(2000);
toast.makeText(this, “Location permission not
locationRequest.setFastestInterval(1000); granted, “ + “restart the app if you want the
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACC
URACY); feature”, Toast.LENGTH_SHORT).show();

}
fusedLocationClient.requestLocationUpdates(locationRequest,
}
} super.onPause();

}
fusedLocationClient.removeLocationUpdates(locationCallb
private class LocationAddressResultReceiver extends
ack);
resultReceiver {
}
locationAddressResultReceiver(Handler handler) {
}
Super(handler);

}
OUTPUT:-
@Override

protected void onReceiveResult(int resultCode, Bundle


resultData) {

if (resultCode == 0) {

log.d(“Address”, “Location null retrying”);

getAddress();

if (resultCode == 1) {

toast.makeText(MainActivity.this, “Address not found, “,


Toast.LENGTH_SHORT).show();

string currentAdd = resultData.getString(“address_result”);

showResults(currentAdd);

private void showResults(String currentAdd) {

currentAddTv.setText(currentAdd);

@Override

protected void onResume() {

super.onResume();

startLocationUpdates();

@Override

protected void onPause() {

Super.onPause();

You might also like