0% found this document useful (0 votes)
6 views3 pages

MAD Model Answer Q4

The document provides a complete Java code example for obtaining GPS location in an Android application. It includes necessary permissions in the AndroidManifest.xml and the implementation of location retrieval in MainActivity.java using FusedLocationProviderClient. The code handles permission requests and displays the latitude and longitude on the screen or shows a toast message if the location is unavailable.

Uploaded by

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

MAD Model Answer Q4

The document provides a complete Java code example for obtaining GPS location in an Android application. It includes necessary permissions in the AndroidManifest.xml and the implementation of location retrieval in MainActivity.java using FusedLocationProviderClient. The code handles permission requests and displays the latitude and longitude on the screen or shows a toast message if the location is unavailable.

Uploaded by

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

Q4.

Get GPS Location in Android (Full Java Code)

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

// MainActivity.java
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;

public class MainActivity extends AppCompatActivity {

FusedLocationProviderClient fusedLocationClient;
TextView locationText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

locationText = findViewById(R.id.locationText);
fusedLocationClient =
LocationServices.getFusedLocationProviderClient(this);

if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {
getLocation();
}
}

private void getLocation() {


fusedLocationClient.getLastLocation()
.addOnSuccessListener(this, location -> {
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
locationText.setText("Latitude: " + lat + ", Longitude: " + lng);
} else {
Toast.makeText(MainActivity.this, "Location not available",
Toast.LENGTH_SHORT).show();
}
});
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1 && grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getLocation();
}
}
}

You might also like