Lab 8
Lab 8
Lab Record - 8
M Sai Mahesh
21039
Exp 9
Aim:
GPS information
Code:
Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="20dp"
android:layout_marginTop="100dp"
android:id="@+id/t1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start GPS service"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/b1"/>
</RelativeLayout>
AndroidManifest.java
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme._8thTask"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
MainActivity.java
package com.example.a8thtask;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
t1=(TextView) findViewById(R.id.t1);
b1=(Button)findViewById(R.id.b1);
LM=(LocationManager)getSystemService(LOCATION_SERVICE);
LL = new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
t1.append("\n" + "Latitude: " + location.getLatitude() + "\tLongitude:"
+location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
// if GPS is not enabled then the following onProviderDisabled functionask you to enable it.
@Override
public void onProviderDisabled(@NonNull String provider) {
// ask the user to enable the GPS
Toast.makeText(getApplicationContext(),"Please enable GPS",
Toast.LENGTH_LONG).show();
// to highlight the user to enable the GPS
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
// this intent goes to enable the location service activation activity
startActivity(intent);
// the activity starts and ask you to enable the GPS
}
};
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
LM.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 5, LL);
// time show how quickly the GPS should update distance shows how far the GPS changes if
it is 0 then just a
// slight movement will update the GPS
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "Please enable GPS", Toast.LENGTH_LONG).show();
}
}
});
}
}
Screenshots:
Result:
This is the required Application.