An Android Application to Locate mobile devices


To find the exact location of a person or any kind of a device we have to use Latitude and Longitude values of that particular object. So using the mobile application that we are going to built, it can be used to get the Latitude and Longitude values according to the location of the mobile device. The values will dynamically change when mobile device location changes.

Let's build the Application

First in order  to create the android application follow the following steps 

1. Create a new android application project



Here in the Application name put what ever name that you would like to use and accordingly the Project Name and the Package Name would also be generated. 


After that, press Next button until you come across the following page and hit finish. 


2. Adding permission

After successfully creating the application, there is a set of permission that needs to be added in order to get the latitude and longitude values. 

So go to the main AndroidManifest.xml page and add the following code just before <application> starts

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>


3. Adjusting the layout

Now let's look at how to create the main interface and how the values will be displayed.

Go to res --> layout --> activity_main.xml,  add text fields and the Graphical Layout should look like

 And in the  activity_main.xml file the complete code should look like

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Measure GPS Cordinates"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="122dp"
        android:text="Latitude"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textLat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_marginLeft="41dp"
        android:layout_toRightOf="@+id/textView2"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_centerVertical="true"
        android:text="Longitude"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textLong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignLeft="@+id/textLat"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />
    

</RelativeLayout>

4. Finally add the following code in the MainActivity.java file 


And the complete code 


package com.example.location;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.widget.TextView;


public class MainActivity extends Activity {
TextView textLat;
TextView textLong;
double pLat;
double pLong;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        textLat = (TextView)findViewById(R.id.textLat);
        textLong = (TextView)findViewById(R.id.textLong);
        
        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new myLocationListeren();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
    }
    
        class myLocationListeren implements LocationListener{

@Override
public void onLocationChanged(Location location) {
if(location != null)
{
pLat = location.getLatitude();
pLong = location.getLongitude();
textLat.setText(Double.toString(pLat));
textLong.setText(Double.toString(pLong));
}
}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}

        }
        
}
   

When you run the code in a real android device it will give values according to device location. When you move the values will also be moved. 

No comments:

Post a Comment