Adi mc4
Adi mc4
Experiment No: -
AIM: Develop an application that uses GPS location information.
Step5: Write java code to handle button clicks and permissions for location.
Name: YASHASHREE DHAKE Roll No: 8 Batch: T1
Step6: Running the project to see the output appear on the screen.
Conclusion: Thus, we successfully implemented the application that uses GPS location
information.
Name: YASHASHREE DHAKE Roll No: 8 Batch: T1
Code: MainActivity.java
package com.example.gps;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.example.gps.R;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
@Override
{ super.onCreate(savedInstanceState);
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
// Find views by ID
buttonGetLocation.setOnClickListener(v -> {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSION_REQUEST_CODE);
} else {
getLocation();
}
});
// ActivityCompat#requestPermissions
// int[] grantResults)
Name: YASHASHREE DHAKE Roll No: 8 Batch: T1
// to handle the case where the user grants the permission. See the documentation
return;
}
fusedLocationClient.getLastLocation()
{ @Override
public void onSuccess(Location location) {
if (location != null) {
} else {
});
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
getLocation();
} else {
}}Activity_main.xml
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Location"
android:textColor="#F4E10B"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.602" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="182dp"
android:layout_height="257dp"
android:layout_marginStart="114dp"
android:layout_marginTop="128dp"
android:layout_marginEnd="115dp"
Name: YASHASHREE DHAKE Roll No: 8 Batch: T1
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/google_maps_logo_2020" />
</androidx.constraintlayout.widget.ConstraintLayout>
Name: YASHASHREE DHAKE Roll No: 8 Batch: T1
OUTPUT:
Generated Button Asking For Permission