0% found this document useful (0 votes)
10 views4 pages

Lab 8

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)
10 views4 pages

Lab 8

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/ 4

20CYS404

Android Application Development

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

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<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" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</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;

public class MainActivity extends AppCompatActivity {


TextView t1;
Button b1;
LocationManager LM;
LocationListener LL;

@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.

You might also like