Experiment-10
Develop a native application that uses the GPS Location information
Program:
MainActivity.java
package com.example.gps;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager)
this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
//Toast.makeText(MainActivity.this,
location.toString(), Toast.LENGTH_LONG).show();
updateLocationDetails(location);
}
@Override
public void onStatusChanged(String provider, int
status, Bundle extras) { }
@Override
public void onProviderEnabled(String provider) { }
@Override
public void onProviderDisabled(String provider) { }
};
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCES
S_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]
{Manifest.permission.ACCESS_FINE_LOCATION},1);
}else{
//
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER
,0,0,locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROV
IDER,0,0,locationListener);
Location lastKnownLocation =
locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVID
ER);
if(lastKnownLocation != null){
updateLocationDetails(lastKnownLocation);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
if(grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED){
checkLocPermission();
}
}
public void checkLocPermission(){
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCES
S_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER
,0,0,locationListener);
}
}
public void updateLocationDetails(Location location){
//Toast.makeText(this, location.toString(),
Toast.LENGTH_SHORT).show();
TextView latitudeView = findViewById(R.id.latitudeView);
TextView longitudeView = findViewById(R.id.longitudeView);
TextView accuracyView = findViewById(R.id.accuracyView);
TextView altitudeView = findViewById(R.id.altitudeView);
TextView addressView = findViewById(R.id.addressView);
latitudeView.setText("Latitude :
"+Double.toString(location.getLatitude()));
longitudeView.setText("Longitude :
"+Double.toString(location.getLongitude()));
accuracyView.setText("Accuracy :
"+Double.toString(location.getAccuracy()));
altitudeView.setText("Altitude :
"+Double.toString(location.getAltitude()));
String address = "Could not locate address..";
Geocoder geocoder = new Geocoder(this,
Locale.getDefault());
try {
List<Address> addressList =
geocoder.getFromLocation(location.getLatitude(),location.getLongitu
de(),1);
if(addressList != null && addressList.size() > 0){
address = "Address :\n";
if(addressList.get(0).getThoroughfare() != null){
address +=
addressList.get(0).getThoroughfare() +"\n";
}
if(addressList.get(0).getLocality() != null){
address += addressList.get(0).getLocality() +
"\n";
}
if(addressList.get(0).getPostalCode() != null){
address += addressList.get(0).getPostalCode() +
"\n";
}
if(addressList.get(0).getCountryName() != null){
address += addressList.get(0).getCountryName();
}
}
}catch (Exception e){
e.printStackTrace();
}
addressView.setText(address);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- <ImageView -->
<!-- android:id="@+id/imageView4" -->
<!-- android:layout_width="wrap_content" -->
<!-- android:layout_height="wrap_content" -->
<!-- android:scaleType="centerCrop" -->
<!-- app:layout_constraintBottom_toBottomOf="parent" -->
<!-- app:layout_constraintEnd_toEndOf="parent" -->
<!-- app:layout_constraintHorizontal_bias="1.0" -->
<!-- app:layout_constraintStart_toStartOf="parent" -->
<!-- app:layout_constraintTop_toTopOf="parent" -->
<!-- app:layout_constraintVertical_bias="0.0" -->
<!-- app:srcCompat="@drawable/mountain" /> -->
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="8dp"
android:text=" LOCATION APP "
android:textColor="@android:color/background_light"
android:textSize="40sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- app:layout_constraintEnd_toStartOf="@+id/imageView4" -->
<!-- app:layout_constraintHorizontal_bias="0.5" -->
<!-- app:layout_constraintStart_toEndOf="@+id/imageView4" -->
<!-- app:layout_constraintTop_toTopOf="@+id/imageView4" /> -->
<!-- <ImageView -->
<!-- android:id="@+id/imageView4" -->
<!-- android:layout_width="wrap_content" -->
<!-- android:layout_height="wrap_content" -->
<!-- android:scaleType="centerCrop" -->
<!-- app:layout_constraintBottom_toBottomOf="parent" -->
<!-- app:layout_constraintEnd_toEndOf="parent" -->
<!-- app:layout_constraintHorizontal_bias="1.0" -->
<!-- app:layout_constraintStart_toStartOf="parent" -->
<!-- app:layout_constraintTop_toTopOf="parent" -->
<!-- app:layout_constraintVertical_bias="0.0" -->
<!-- app:srcCompat="@drawable/mountain" /> -->
<TextView android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp" android:layout_marginEnd="8dp"
android:text="HIKER APP"
android:textColor="@android:color/background_light"
android:textSize="40sp" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="2dp"/>
<!-- app:layout_constraintEnd_toStartOf="@+id/imageView4" -->
<!-- app:layout_constraintHorizontal_bias="0.5" -->
<!-- app:layout_constraintStart_toEndOf="@+id/imageView4" -->
<!-- app:layout_constraintTop_toTopOf="@+id/imageView4" /> -->
<TextView android:id="@+id/latitudeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp" android:layout_marginTop="25dp"
android:text="Latitude"
android:textColor="@android:color/background_light"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3"/>
<TextView android:id="@+id/longitudeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp" android:layout_marginTop="25dp"
android:text="Longitude"
android:textColor="@android:color/background_light"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/latitudeView"/>
<TextView android:id="@+id/accuracyView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp" android:layout_marginTop="25dp"
android:text="Accuracy"
android:textColor="@android:color/background_light"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/longitudeView"/>
<TextView android:id="@+id/altitudeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp" android:layout_marginTop="25dp"
android:text="Altitude"
android:textColor="@android:color/background_light"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/accuracyView"/>
<TextView android:id="@+id/addressView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp" android:layout_marginTop="20dp"
android:text="Address\nSector 13, Nerul\nNavii Mumbai"
android:textColor="@android:color/background_light"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/altitudeView"/>
</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gps">
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<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/Theme.GPS">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"
/>
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>