B36 Exp7
B36 Exp7
PART A
A.1 Aim: To develop an android application that uses GPS Location Information.
A.2 Objectives: To introduce students with various tools like Android Studio, NS2, Wire-shark, Cisco
packet tracer, WAP supported browser etc.
A.3 Outcome: After successful completion of this experiment students will be able to
develop an android application that uses GPS Location Information. A.4Theory:
SOFTWARE:
• Android Studio
• Gradle
DESCRIPTION:
4. Enter the package name. package name must be two word separated by comma and click finish
5. Go to package explorer in the left hand side and select our project.
6. Go to res folder and select layout. Double click the main.xml file
INPUT:
MAINACTIVITY.JAVA
package com.gpslocation;
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnShowLocation=(Button)findViewById(R.id.show_Location);
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub gps=new
GPStrace(MainActivity.this);
if(gps.canGetLocation()) { double
latitude=gps.getLatitude(); double
longitude=gps.getLongtiude();
Toast.makeText(getApplicationContext(),"Your Location is
\nLat:"+latitude+"\nLong:"+longitude, Toast.LENGTH_LONG).show();
}
else { gps.showSettingAlert();
}
}
});
}
}
ACTIVITY_MAIN.XML
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"andro
id:id="@+id/relativeLayout1" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="@+id/show_Location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show_Location"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
GPSTRACE.JAVA
package com.gpslocation;
LocationManagerlocationManager; public
GPStrace(Context context) {
this.context=context; getLocation();
}
public Location getLocation() { try { locationManager=(LocationManager)
context.getSystemService(LOCATION_SERVICE);
isGPSEnabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER
);
isNetworkEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_ PROVIDER);
if(!isGPSEnabled&& !isNetworkEnabled) {
}
else { this.canGetLocation=true;
if(isNetworkEnabled) {
locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
}
if(locationManager!=null) {
location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVI DER);
if(location !=null) { latitude=location.getLatitude();
longtitude=location.getLongitude();
}
}
}
if(isGPSEnabled) { if(location==null)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIM E_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this); if(locationManager!=null) {
location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
;
if(location!=null) { latitude=location.getLatitude();
longtitude=location.getLongitude();
}
}
}
}
}
catch(Exception e) {
e.printStackTrace();
}
return location;
}
<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.GPSLocation">
<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>
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
OUTPUT:
PART B
(Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded on the Blackboard or emailed to the concerned lab
in charge faculties at the end of the practical in case the there is no Black board access
available)
Grade:
} catch (IOException e) {
runOnUiThread(() -> Toast.makeText(this, "Error fetching coordinates",
Toast.LENGTH_SHORT).show());
}
}).start();
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<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.Exp7"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
B.4 Conclusion:
The experiment demonstrated the practical implementation of GPS-based location tracking in Android
applications. Students gained experience with integrating Android's Location API and handling
permissions for accessing user location. By completing this project, they understood how real-time
location services work and how they can be used in mobile applications for various purposes like
navigation, tracking, and location-based services.
• • Install Android Studio, Java Development Kit (JDK), and Android SDK.
• Configure Gradle dependencies.