Showing posts with label Android App Course. Show all posts
Showing posts with label Android App Course. Show all posts

Sunday, July 15, 2012

Android - Connection to the internet 3

Hello



This post demonstrate client\server interaction using JSON.

The client is android app and the server is asp.net based.





1. Android Client

Java supports JSON in terms of the classes :

  • JSONArray
  • JSONObject


2. ASP.Net Server
.Net supports JSON in terms of DataContractJsonSerializer




3. Source Sample

JSONClient.zip
JSONWebSite.zip


This sample includes :


1. The client send GET request to an ASP.Net server


2. The server responds with JSON array :

 [ {UserId="John" ,Latitude = 33.33 , Longitude = 44.56 ,Location = "some place 1" },
    {UserId="Jim" ,Latitude = 44.33 , Longitude = 55.56 ,Location = "some place 2" }]
  which is written via  DataContractJsonSerializer and  Response.Write to the client


3. The client parse the JSON array via classes JSONArray and JSONObject and write it to a TextView





Client
uses HttpURLConnection as done in android-connection-to-internet-1

The new stuf here is the parsing of the JSON array

protected void onPostExecute(Object result) {
       try {
       String strOut="";
    JSONArray json = new JSONArray((String)result);
    
    for (int i = 0; i < json.length(); ++i) {
        JSONObject rec = json.getJSONObject(i);
        strOut += String.format("UserId : %s ,Latitude : %f ,Longitude : %f ,Location : %s\n", 
          rec.getString("UserId"),rec.getDouble("Latitude"),
          rec.getDouble("Longitude"),rec.getString("Location"));
       
    }
    textViewMessage.setText(strOut);
       }
    catch (JSONException e) {
    textViewMessage.setText(e.getMessage());
   }
            
       }

MainActivity.java
This is basically like  android-connection-to-internet-1 beside the JSON stuff


package com.example.jsonclient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        urlText = (EditText) findViewById(R.id.editTextURL);
        textViewURL = (TextView) findViewById(R.id.textViewURL);
        textViewMessage = (TextView) findViewById(R.id.textViewMessage);
    }

    

    
   // When user clicks button, calls AsyncTask.
   // Before attempting to fetch the URL, makes sure that there is a network connection.
    public void myClickHandler(View view) {
        // Gets the URL from the UI's text field.
        String stringUrl = urlText.getText().toString();
        ConnectivityManager connMgr = (ConnectivityManager) 
            getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            new DownloadWebpageText().execute(stringUrl);
        } else {
            textViewMessage.setText("No network connection available.");
        }
    }

     // Uses AsyncTask to create a task away from the main UI thread. This task takes a 
     // URL string and uses it to create an HttpUrlConnection. Once the connection
     // has been established, the AsyncTask downloads the contents of the web page as
     // an InputStream. Finally, the InputStream is converted into a string, which is
     // displayed in the UI by the AsyncTask's onPostExecute method.
     private class DownloadWebpageText extends AsyncTask {
      @Override
      protected String doInBackground(Object... urls) {
              
            // params comes from the execute() call: params[0] is the url.
            try {
                return downloadUrl((String) urls[0]);
            } catch (IOException e) {
                return "Unable to retrieve web page. URL may be invalid.";
            }
        }
        // onPostExecute displays the results of the AsyncTask.
      @Override
      protected void onPostExecute(Object result) {
       try {
       String strOut="";
    JSONArray json = new JSONArray((String)result);
    
    for (int i = 0; i < json.length(); ++i) {
        JSONObject rec = json.getJSONObject(i);
        strOut += String.format("UserId : %s ,Latitude : %f ,Longitude : %f ,Location : %s\n", 
          rec.getString("UserId"),rec.getDouble("Latitude"),
          rec.getDouble("Longitude"),rec.getString("Location"));
       
    }
    textViewMessage.setText(strOut);
       }
    catch (JSONException e) {
    textViewMessage.setText(e.getMessage());
   }
            
       }
    }
  // Given a URL, establishes an HttpUrlConnection and retrieves
  // the web page content as a InputStream, which it returns as
  // a string.
  private String downloadUrl(String myurl) throws IOException {
      InputStream is = null;
      
           
      try {
          URL url = new URL(myurl);
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setReadTimeout(10000 /* milliseconds */);
          conn.setConnectTimeout(15000 /* milliseconds */);
          conn.setRequestMethod("GET");
          conn.setDoInput(true);
          // Starts the query
          conn.connect();
          int response = conn.getResponseCode();
          Log.d(DEBUG_TAG, "The response is: " + response);
          is = conn.getInputStream();

          // Convert the InputStream into a string
          BufferedReader in = new BufferedReader(new InputStreamReader(is));
         String line;
         String page = "";
         line = in.readLine();
         String  contentAsString="";
         while (line != null)
         {
        contentAsString = contentAsString + line;
           line = in.readLine();
         }
          
          
          return contentAsString;
          
      // Makes sure that the InputStream is closed after the app is
      // finished using it.
      } finally {
          if (is != null) {
              is.close();
          } 
      }
  }
    
//Reads an InputStream and converts it to a String.
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
   Reader reader = null;
   reader = new InputStreamReader(stream, "UTF-8");        
   char[] buffer = new char[len];
   reader.read(buffer);
   return new String(buffer);
}
  
    private static final String DEBUG_TAG = "HttpExample";
    private EditText urlText;
    private TextView textViewURL;
    private TextView textViewMessage;
}




Server

The server is ASP.Net based.
It create JSON array via DataContractJsonSerializer and send to the client using Response.Write .
The server uses helper class name JsonHelper 

main.aspx



CJsonHelper.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Web;
using System.IO;
using System.Text;  

/// 
/// Summary description for Class1
/// 
public class JsonHelper
{
    /// 
    /// JSON Serialization
    /// 
    public static string JsonSerializer<T>(T t)
    {
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
        MemoryStream ms = new MemoryStream();
        ser.WriteObject(ms, t);
        string jsonString = Encoding.UTF8.GetString(ms.ToArray());
        ms.Close();
        return jsonString;
    }
    /// 
    /// JSON Deserialization
    /// 
    public static T JsonDeserialize<T>(string jsonString)
    {
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
        T obj = (T)ser.ReadObject(ms);
        return obj;
    }
}

CTableRow.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// 
/// Summary description for Class1
/// 
public class CTableRow
{
    public string UserId;
    public double Latitude;
    public double Longitude;
    public string Location;
}
Run the application to create :




Click the Get button to crete




These are the exact info inserted by the server


Nathan

Friday, July 13, 2012

Android - SQLite

Hello


Motivation



Android default Database engine is Lite. SQLite is a lightweight transactional database engine that occupies a small amount of disk storage and memory, so it's a perfect choice for creating databases on many mobile operating systems such as Android, iOS.



Class SQLiteDatabase




Exposes methods to manage a SQLite database.
SQLiteDatabase has methods to create, delete, execute SQL commands, and perform other common database management tasks.
Database names must be unique within an application, not across all applications.

In addition to SQLite's default BINARY collator, Android supplies two more, LOCALIZED, which changes with the system's current locale, and UNICODE, which is the Unicode Collation Algorithm and not tailored to the current locale.





Class SQLiteDatabase methods






openOrCreateDatabase : 

  • open \ create data base


execSQL



rawQuery

  • Runs the provided SQL and returns a Cursor over the result set.


close

  • Releases a reference to the object, closing the object if the last reference was released



Source sample


SQLite.zip

This sample illustrate :

  • DataBase and Table creation
  • Insert row into table
  • Delete all rows from table
  • Read all rows from data base


Data base : LocationDbName
Table_1 Schema :

  • VARCHAR, UserId
  • REAL - Longitude
  • REAL - Latitude
  • VARCHAR - Location
  • INTEGER- DateTime (time in milisecond sience1, 1970, 00:00:00 GMT)
remark :



SQLite.java

package com.example.sqllite;

import java.util.Date;


import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class SQLite extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textViewMessage = (TextView) findViewById(R.id.textViewMessage);
     try {
   m_locationDB =  openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
  } catch (Exception e) {
         textViewMessage.setText(e.getMessage());
        if(m_locationDB!= null)
          m_locationDB.close();
  }
       }
    
    
    private void mFillDbsTable(){
         Date dt = new Date();
         m_locationDB.execSQL("INSERT INTO " +
       TABLE_NAME +
       " Values ('Yosi',33, 44,'Hyarkon 15 Tel Aviv Israel',0);");
         
         m_locationDB.execSQL("INSERT INTO " +
           TABLE_NAME +
           String.format(" Values ('John',11, 15,'Times Square New York U.S.A',%d);",dt.getTime()));
    }

   
    
    public void insertClick(View view)
    {
        try {
         String strSqlStatement;
         strSqlStatement = String.format("CREATE TABLE IF NOT EXISTS %s (%s VARCHAR , %s REAL, %s REAL,%s VARCHAR , %s INTEGER);",
           TABLE_NAME,Const.strUserIdKey,Const.strLongitudeKey,
           Const.strLatitudeKey,Const.strLocationKey,Const.strDateTimeKey);
         m_locationDB.execSQL(strSqlStatement);
         
         mFillDbsTable();
         } catch (Exception se ) {
          textViewMessage.setText(se.getMessage());
        }
        
        }
    
    public void deleteClick(View view)
    {
      try {
       m_locationDB.execSQL(String.format("DELETE FROM  %s;",TABLE_NAME));
          }
      catch (Exception se ) {
          textViewMessage.setText(se.getMessage());
        }
    }
    
    public void readClick(View view)
    {
      try {
       Cursor c = m_locationDB.rawQuery(String.format("SELECT *  FROM %s;",TABLE_NAME),null);
            Date dt = new Date();
            String strRow="";
           textViewMessage.setText("");
           
          if (c != null ) {
           if  (c.moveToFirst()) {
            do {
             String strUserId = c.getString(c.getColumnIndex(Const.strUserIdKey));
                double fLongitude = c.getDouble(c.getColumnIndex(Const.strLongitudeKey));
                double fLatitiud = c.getDouble(c.getColumnIndex(Const.strLatitudeKey));
                String strLocation = c.getString(c.getColumnIndex(Const.strLocationKey));
              dt.setTime(c.getLong(c.getColumnIndex(Const.strDateTimeKey)));
              
             strRow += (String.format("%s,%f,%f,%s,%s\n",
                strUserId,fLongitude,fLatitiud,strLocation,dt.toString()));
              
            }while (c.moveToNext());
            
            textViewMessage.setText(strRow);
           } 
          }
         } catch (Exception se ) {
          textViewMessage.setText(se.getMessage());
        }
    }
    

    private final String DB_NAME = "LocationDbName";
 private final String TABLE_NAME = "Table_1";
    SQLiteDatabase m_locationDB = null;
    TextView textViewMessage;
}


Run the application to create


Click on Insert then click Read :


Click on Delete then click Read :





The following app was published using this post.

Nathan

Thursday, July 5, 2012

Android - Connection to the internet 2

Hello

This post demonstarte client\server interaction.

The client is android app and the server is asp.net based.




Client


The client use HttpClient  , HttpPost and UrlEncodedFormEntity to create form based request to an asp based server.
The form has two pairs :

  • key - "userName" , value - "Nathan"
  • key - "userPassword" , value - "p456"


     HttpClient client1 = new DefaultHttpClient();

     HttpPost request = new HttpPost(urlText.getText().toString());

     String user = "userName",pass = "userPassword";

     List<NameValuePair> postParameters = new ArrayList<NameValuePair>(2);
     postParameters.add(new BasicNameValuePair(user, "Nathan"));
     postParameters.add(new BasicNameValuePair(pass, "p456"));
     try {
         request.setEntity(new UrlEncodedFormEntity(postParameters));
         UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
         request.setEntity(formEntity);
         
         HttpResponse response;
         response = client1.execute(request);

The server response  is processed as followes


 BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
         String line;
         String page = "";
         line = in.readLine();
         while (line != null)
         {
             page = page + line;
             line = in.readLine();
         }
           textViewMessage.setText(page);

The response is actually the html file filled with user name and password as described in asp file

Server



The server is a simple IIS as described in HTML based Web Site (chap 2 web beginners) but an asp file is used.
The asp file access the keys userName, userPassword and return html as response





Source sample



HttpPost.zip


Run the application to create :



Click get to create



Note that the server response is the html file as described in second.aspx but the value of userName and userPassword which were sent by the client are used

remarks :

  • the host ip is not shown.
  • in case the host is your local computer then you will have to disable the firewall
The following app was published using this post.

Nathan

Wednesday, July 4, 2012

Android - Connection to the internet 1

Hello


This post shows you how to implement a simple application that connects to the network. It explains some of the best practices you should follow in creating even the simplest network-connected app.
Note that to perform the network operations described in this lesson, your application manifest must include the following permissions:



<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />




Choose an HTTP Client

Most network-connected Android apps use HTTP to send and receive data. Android includes two HTTP clients: HttpURLConnection and Apache HttpClient. Both support HTTPS, streaming uploads and downloads, configurable timeouts, IPv6, and connection pooling. We recommend using HttpURLConnection for applications targeted at Gingerbread and higher. For more discussion of this topic, see the blog post Android's HTTP Clients.



Check the Network Connection

Before your app attempts to connect to the network, it should check to see whether a network connection is available using getActiveNetworkInfo() and isConnected(). Remember, the device may be out of range of a network, or the user may have disabled both Wi-Fi and mobile data access. 

// When user clicks button, calls AsyncTask.
   // Before attempting to fetch the URL, makes sure that there is a network connection.
    public void myClickHandler(View view) {
        // Gets the URL from the UI's text field.
        String stringUrl = urlText.getText().toString();
        ConnectivityManager connMgr = (ConnectivityManager) 
            getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            // fetch data
        } else {
            // display error
        }
    }


Perform Network Operations on a Seprate Thread


Network operations can involve unpredictable delays. To prevent this from causing a poor user experience, always perform network operations on a separate thread from the UI. The AsyncTask class provides one of the simplest ways to fire off a new task from the UI thread. For more discussion of this topic, see the blog post Multithreading For Performance.
In the following snippet, the myClickHandler() method invokes new DownloadWebpageTask().execute(stringUrl). The DownloadWebpageTask class is a subclass of AsyncTaskDownloadWebpageTask implements the following AsyncTask methods:
  • doInBackground() executes the method downloadUrl(). It passes the web page URL as a parameter. The method downloadUrl() fetches and processes the web page content. When it finishes, it passes back a result string.
  • onPostExecute() takes the returned string and displays it in the UI.
public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        urlText = (EditText) findViewById(R.id.editTextURL);
        urlText.setText("http://www.oracle.com/");
        textViewURL = (TextView) findViewById(R.id.textViewURL);
        textViewMessage = (TextView) findViewById(R.id.textViewMessage);
    }

    

    
   // When user clicks button, calls AsyncTask.
   // Before attempting to fetch the URL, makes sure that there is a network connection.
    public void myClickHandler(View view) {
        // Gets the URL from the UI's text field.
        String stringUrl = urlText.getText().toString();
        ConnectivityManager connMgr = (ConnectivityManager) 
            getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            new DownloadWebpageText().execute(stringUrl);
        } else {
            textViewMessage.setText("No network connection available.");
        }
    }

     // Uses AsyncTask to create a task away from the main UI thread. This task takes a 
     // URL string and uses it to create an HttpUrlConnection. Once the connection
     // has been established, the AsyncTask downloads the contents of the web page as
     // an InputStream. Finally, the InputStream is converted into a string, which is
     // displayed in the UI by the AsyncTask's onPostExecute method.
     private class DownloadWebpageText extends AsyncTask {
      @Override
      protected String doInBackground(Object... urls) {
              
            // params comes from the execute() call: params[0] is the url.
            try {
                return downloadUrl((String) urls[0]);
            } catch (IOException e) {
                return "Unable to retrieve web page. URL may be invalid.";
            }
        }
        // onPostExecute displays the results of the AsyncTask.
      @Override
      protected void onPostExecute(Object result) {
            textViewMessage.setText((String)result);
       }
    }
...
}


Connect and Download Data

In your thread that performs your network transactions, you can use HttpURLConnection to perform a GET and download your data. After you call connect(), you can get an InputStream of the data by calling getInputStream().
In the following snippet, the doInBackground() method calls the method downloadUrl(). The downloadUrl() method takes the given URL and uses it to connect to the network via HttpURLConnection. Once a connection has been established, the app uses the method getInputStream() to retrieve the data as an InputStream.
// Given a URL, establishes an HttpUrlConnection and retrieves
// the web page content as a InputStream, which it returns as
// a string.
private String downloadUrl(String myurl) throws IOException {
    InputStream is = null;
    // Only display the first 500 characters of the retrieved
    // web page content.
    int len = 500;
        
    try {
        URL url = new URL(myurl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        // Starts the query
        conn.connect();
        int response = conn.getResponseCode();
        Log.d(DEBUG_TAG, "The response is: " + response);
        is = conn.getInputStream();

        // Convert the InputStream into a string
        String contentAsString = readIt(is, len);
        return contentAsString;
        
    // Makes sure that the InputStream is closed after the app is
    // finished using it.
    } finally {
        if (is != null) {
            is.close();
        } 
    }
}
Note that the method getResponseCode() returns the connection's status code. This is a useful way of getting additional information about the connection. A status code of 200 indicates success.



Convert the InputStream to a String

An InputStream is a readable source of bytes. Once you get an InputStream, it's common to decode or convert it into a target data type. For example, if you were downloading image data, you might decode and display it like this:
InputStream is = null;
...
Bitmap bitmap = BitmapFactory.decodeStream(is);
ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setImageBitmap(bitmap);
In the example shown above, the InputStream represents the text of a web page. This is how the example converts the InputStream to a string so that the activity can display it in the UI:
// Reads an InputStream and converts it to a String.
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
    Reader reader = null;
    reader = new InputStreamReader(stream, "UTF-8");        
    char[] buffer = new char[len];
    reader.read(buffer);
    return new String(buffer);
}


Source sample
ConnectToTheInternet1.zip

The sample is based on the above code snippets.
The sample use GET command to get htmal page.

Run the application :



Click the Get button to produce



The following app was published using this post.

source - http://developer.android.com/training/basics/network-ops/connecting.html

Nathan

Monday, July 2, 2012

Android - Location Manager

Hello


Users bring their mobile devices with them almost everywhere. One of the unique features available to mobile applications is location awareness. Knowing the location and using the information wisely can bring a more contextual experience to your users.
This class teaches you how to incorporate location based services in your Android application. You'll learn a number of methods to receive location updates and related best practices.

1. Using the Location Manager

Before your application can begin receiving location updates, it needs to perform some simple steps to set up access. In this lesson, you'll learn what these steps entail.

Declare Proper Permissions in Android Manifest

The first step of setting up location update access is to declare proper permissions in the manifest. If permissions are missing, the application will get a SecurityException at runtime.
Depending on the LocationManager methods used, either ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission is needed. For example, you need to declare the ACCESS_COARSE_LOCATION permission if your application uses a network-based location provider only. The more accurate GPS requires the ACCESS_FINE_LOCATION permission. Note that declaring the ACCESS_FINE_LOCATION permission implies ACCESS_COARSE_LOCATION already.
Also, if a network-based location provider is used in the application, you'll need to declare the internet permission as well.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />


Get a Reference to LocationManager

LocationManager is the main class through which your application can access location services on Android. Similar to other system services, a reference can be obtained from calling the getSystemService() method. If your application intends to receive location updates in the foreground (within an Activity), you should usually perform this step in the onCreate() method.
LocationManager locationManager =
        (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

Pick a Location Provider

While not required, most modern Android-powered devices can receive location updates through multiple underlying technologies, which are abstracted to an application as LocationProvider objects. Location providers may have different performance characteristics in terms of time-to-fix, accuracy, monetary cost, power consumption, and so on. Generally, a location provider with a greater accuracy, like the GPS, requires a longer fix time than a less accurate one, such as a network-based location provider.
Depending on your application's use case, you have to choose a specific location provider, or multiple providers, based on similar tradeoffs. For example, a points of interest check-in application would require higher location accuracy than say, a retail store locator where a city level location fix would suffice. The snippet below asks for a provider backed by the GPS.
LocationProvider provider =
        locationManager.getProvider(LocationManager.GPS_PROVIDER);
Alternatively, you can provide some input criteria such as accuracy, power requirement, monetary cost, and so on, and let Android decide a closest match location provider. The snippet below asks for a location provider with fine accuracy and no monetary cost. Note that the criteria may not resolve to any providers, in which case a null will be returned. Your application should be prepared to gracefully handle the situation.


// Retrieve a list of location providers that have fine accuracy, no monetary cost, etc
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(false);
...
String providerName = locManager.getBestProvider(criteria, true);

// If no suitable provider is found, null is returned.
if (providerName != null) {
   ...
}

Verify the Location Provider is Enabled


Some location providers such as the GPS can be disabled in Settings. It is good practice to check whether the desired location provider is currently enabled by calling the isProviderEnabled() method. If the location provider is disabled, you can offer the user an opportunity to enable it in Settings by firing an Intent with the ACTION_LOCATION_SOURCE_SETTINGS action.
In this sample the GPS provider is queried and the user is given AlertDialog in case the provider is not enabled
  

Sample source
This sample demonstrate simple use of LocationManager using GPS service.
It uses LocationManager inside onStart().  Longitude\Latitude are writen to the GUI in case GPS is enabled ,otherwise a AlertDialog is issued and the refresh button is disabled.
The user can click the Refresh button to get fresh location

LocaltionApp.zip

MainActivity.java

package com.example.localtionapp;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    
    @Override
    protected void onStart() {
        super.onStart();

        try {
   // This verification should be done during onStart() because the system calls
   // this method when the user returns to the activity, which ensures the desired
   // location provider is enabled each time the activity resumes from the stopped state.
   m_LocationManager =
           (LocationManager) getSystemService(Context.LOCATION_SERVICE);
   final boolean gpsEnabled = 
     m_LocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

   if (gpsEnabled) 
   {
    updateGuiLocation();
   }
   else
   {
    Button button = (Button) findViewById(R.id.ButtonRefresh);
    button.setEnabled(false);
    alert();
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
    }


 private void updateGuiLocation() {
  double fLatitiude=0,fLongitude=0;
  Location loc = m_LocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

  if(loc != null) // can be null even if GPS is enabled !
  {
   fLatitiude = loc.getLatitude();
   fLongitude= loc.getLongitude();
  }
  EditText editText = (EditText) findViewById(R.id.editTextLatitiude);
  editText.setText(String.format("%1$,f", fLatitiude));
  
  editText = (EditText) findViewById(R.id.editTextLongitiude);
  editText.setText(String.format("%1$,f", fLongitude));
 }

 @SuppressWarnings("deprecation")
 private void alert() {
  showDialog(ALERT_DIALOG_ID);
 }
    
 @Override
    protected  Dialog onCreateDialog(int id, Bundle args)
 {
        switch(id)
        {
        case ALERT_DIALOG_ID:
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Please enable GPS service");
            builder.setTitle("GPS problem");
            AlertDialog alert = builder.create();
            return(alert);
        }
        
        return null;
 }
 
 /** Called when the user selects the Refresh button */
 public void Refresh(View view) {
  updateGuiLocation();
 }
 
    final int ALERT_DIALOG_ID = 0;
    LocationManager m_LocationManager;
  }


activity_main.xml


<LinearLayout 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:orientation="vertical">

 
    <TextView
        android:id="@+id/textViewLatitiude"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Latitiude"
        tools:context=".MainActivity" />
     
        <EditText
        android:id="@+id/editTextLatitiude"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:context=".MainActivity" />
     
     
 
        <TextView
        android:id="@+id/textViewLongitiude"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Longitiude"
        tools:context=".MainActivity" />
     
     
<EditText
        android:id="@+id/editTextLongitiude"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:context=".MainActivity" />
     
     <Button
        android:id="@+id/ButtonRefresh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_refresh"
        android:onClick="Refresh"  />

</LinearLayout>


AndroidManifest.xml



Run the application to create :



While running the app on your android phone you should be able to see the Latitude \ Longitude.
Clicking Refresh bring fresh location.

2. Obtaining the Current Location Using LocationListener

After setting up your application to work with LocationManager, you can begin to obtain location updates


Set Up the Location Listener

The LocationManager class exposes a number of methods for applications to receive location updates. In its simplest form, you register an event listener, identify the location manager from which you'd like to receive location updates, and specify the minimum time and distance intervals at which to receive location updates. The onLocationChanged() callback will be invoked with the frequency that correlates with time and distance intervals.
In the sample code snippet below, the location listener is set up to receive notifications at least every 10 seconds and if the device moves by more than 10 meters. The other callback methods notify the application any status change coming from the location provider.


private
final LocationListener listener = new LocationListener() { @Override public void onLocationChanged(Location location) { // A new location update is received. Do something useful with it. ...... } ... }; mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, // 10-second interval. 10, // 10 meters. listener);


Handle Multiple Sources of Location Updates

Generally speaking, a location provider with greater accuracy (GPS) requires a longer fix time than one with lower accuracy (network-based). If you want to display location data as quickly as possible and update it as more accurate data becomes available, a common practice is to register a location listener with both GPS and network providers. In the onLocationChanged() callback, you'll receive location updates from multiple location providers that may have different timestamps and varying levels of accuracy. You'll need to incorporate logic to disambiguate the location providers and discard updates that are stale and less accurate. 


Use getLastKnownLocation() Wisely

The setup time for getting a reasonable location fix may not be acceptable for certain applications. You should consider calling the getLastKnownLocation() method which simply queries Android for the last location update previously received by any location providers. Keep in mind that the returned location may be stale. You should check the timestamp and accuracy of the returned location and decide whether it is useful for your application. If you elect to discard the location update returned from getLastKnownLocation() and wait for fresh updates from the location provider(s), you should consider displaying an appropriate message before location data is received.



Terminate Location Updates

When you are done with using location data, you should terminate location update to reduce unnecessary consumption of power and network bandwidth. For example, if the user navigates away from an activity where location updates are displayed, you should stop location update by calling removeUpdates() in onStop(). (onStop() is called when the activity is no longer visible. If you want to learn more about activity lifecycle, read up on the Stopping and Restarting an Activity lesson.
protected void onStop() {
    super.onStop();
    mLocationManager.removeUpdates(listener);
}
Note: For applications that need to continuously receive and process location updates like a near-real time mapping application, it is best to incorporate the location update logic in a background service and make use of the system notification bar to make the user aware that location data is being used.


3. Displaying the Location Address



As shown in previous section, location updates are received in the form of latitude and longitude coordinates. While this format is useful for calculating distance or displaying a pushpin on a map, the decimal numbers make no sense to most end users. If you need to display a location to user, it is much more preferable to display the address instead.


Perform Reverse Geocoding

Reverse-geocoding is the process of translating latitude longitude coordinates to a human-readable address. The Geocoder API is available for this purpose. Note that behind the scene, the API is dependent on a web service. If such service is unavailable on the device, the API will throw a "Service not Available exception" or return an empty list of addresses. A helper method called isPresent() was added in Android 2.3 (API level 9) to check for the existence of the service.
The following code snippet demonstrates the use of the Geocoder API to perform reverse-geocoding. 
        // Bypass reverse-geocoding if the Geocoder service is not available on the
        // device. The isPresent() convenient method is only available on Gingerbread or above.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD && Geocoder.isPresent()) {
            // ;
        }
};

// AsyncTask encapsulating the reverse-geocoding API.  Since the geocoder API is blocked,
// we do not want to invoke it from the UI thread.
private class ReverseGeocodingTask extends AsyncTask<Location, Void, Void> {
    Context mContext;

    public ReverseGeocodingTask(Context context) {
        super();
        mContext = context;
    }

    @Override
    protected Void doInBackground(Location... params) {
        Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());

        Location loc = params[0];
        List<Address> addresses = null;
        try {
            // Call the synchronous getFromLocation() method by passing in the lat/long values.
            addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
        } catch (IOException e) {
            e.printStackTrace();
            // Update UI field with the exception.
            Message.obtain(mHandler, UPDATE_ADDRESS, e.toString()).sendToTarget();
        }
        if (addresses != null &s;&s; addresses.size() > 0) {
            Address address = addresses.get(0);
            // Format the first line of address (if available), city, and country name.
            String addressText = String.format("%s, %s, %s",
                    address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                    address.getLocality(),
                    address.getCountryName());
            // Update the UI via a message handler.
            Message.obtain(mHandler, UPDATE_ADDRESS, addressText).sendToTarget();
        }
        return null;
    }
}



Providing Mock Location Data


Emulator

As you develop your application, you'll certainly need to test how well your model for obtaining user location works. This is most easily done using a real Android-powered device. If, however, you don't have a device, you can still test your location-based features by mocking location data in the Android emulator. There are three different ways to send your application mock location data: using Eclipse, DDMS, or the "geo" command in the emulator console.

Remarks :

  • Providing mock location data is injected as GPS location data, so you must request location updates from GPS_PROVIDER in order for mock location data to work.
  • Make sure you create AVD with GPS support


Using Eclipse

Select Window > Show View > Other > Emulator Control.
In the Emulator Control panel, enter GPS coordinates under Location Controls as individual lat/long coordinates, with a GPX file for route playback, or a KML file for multiple place marks. (Be sure that you have a device selected in the Devices panel—available from Window > Show View > Other > Devices.)







Using the "geo" command in the emulator console

To send mock location data from the command line:
  1. Launch your application in the Android emulator and open a terminal/console in your SDK's /tools directory.
  2. Connect to the emulator console:
    telnet localhost <console-port>
  3. Send the location data:
    • geo fix to send a fixed geo-location.
      This command accepts a longitude and latitude in decimal degrees, and an optional altitude in meters. For example:
      geo fix -121.45356 46.51119 4392
    • geo nmea to send an NMEA 0183 sentence.
      This command accepts a single NMEA sentence of type '$GPGGA' (fix data) or '$GPRMC' (transit data). For example:
      geo nmea $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62
remark :

  • look at the console window to see the connect port



Test Provider
One can use the following :
void addTestProvider (String name, boolean requiresNetwork, boolean requiresSatellite, boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude, boolean supportsSpeed, boolean supportsBearing, int powerRequirement, int accuracy) :

  • should be called once or test for existence


setTestProviderEnabled(String provider, boolean enabled)

setTestProviderLocation(String provider, Location loc)

removeTestProvider (String provider)

One must add
 <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
to the AndroidManifest.xml inside the manifest tag

code for Mock appears here 


The following app was published using this post.

source - http://developer.android.com/training/basics/location/index.html

Nathan