Hi ranjan,

Following is a working example ( when used on real phone ) once the
program is started and the GPS get a fix, it will display some of the
current GPS location details including the speed in meters per second
and kilometers per hour of the device.

Regards Gary

----------------------------------------------------------------------------------------------

Make sure to set <uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"></uses-
permission>
in the manifest file -

-----------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
     package="com.GPSDemo"
     android:versionCode="1"
     android:versionName="1.0">
   <application android:icon="@drawable/icon" android:label="@string/
app_name">
       <activity android:name=".GPSDemo"
                 android:label="@string/app_name">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category
android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>

   </application>
   <uses-sdk android:minSdkVersion="3" />

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

--------------------------------- example code
-------------------------------------------

package com.GPSDemo;

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

public class GPSDemo extends Activity {

       TextView tv = null;

       GPS mGPS = null;;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       tv = new TextView(getApplicationContext());

       setContentView(tv);
   }

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

               if ( mGPS != null )
               {
                       mGPS.stopLocationUpdates();
               }
   }

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

               mGPS = new GPS( this.getApplicationContext() );
   }

   ///////////////////////////

   class GPS implements LocationListener { // as inner class...

       LocationManager lm = null;

       Location actualLocation = null;

       public GPS( Context context )
       {
               lm = (LocationManager)
context.getSystemService(Context.LOCATION_SERVICE);

               if (lm != null) {

                       actualLocation =
lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

               }

               startLocationUpdates();
       }

       public void startLocationUpdates() {
               lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
500l, 0, this);
       }

       public void stopLocationUpdates() {
               lm.removeUpdates(this);
       }

       public void onLocationChanged(Location updatedLocation) {
               actualLocation = updatedLocation;

               String gpsDetails = getGPSString();

               if ( gpsDetails != null)
               {
                       tv.setText( gpsDetails );
               }

       }

       public void onProviderEnabled(String provider) {

       }

       public void onProviderDisabled(String provider) {

       }

       public void onStatusChanged(String provider, int iUnknown,
Bundle
buUnknown) {

       }

       public String getGPSString() {

               String clock = "C" + new java.util.Date().toString();

               String loc = null;

               if (actualLocation != null) {

                       StringBuffer sb = new StringBuffer();

                       sb.append("N"
                                       +
Location.convert(actualLocation.getLatitude(),
 
Location.FORMAT_SECONDS));
                       sb.append(" E"
                                       +
Location.convert(actualLocation.getLongitude(),
 
Location.FORMAT_SECONDS));

                       //if (actualLocation.hasAltitude() == true)
                       {
                               sb.append(" A" +
actualLocation.getAltitude());
                       }

                       //if (actualLocation.hasAccuracy() == true)
                       {
                               sb.append(" D" +
round(actualLocation.getAccuracy()));
                       }

                       //if (actualLocation.hasSpeed() == true)
                       {
                               float kph = round((float)
(actualLocation.getSpeed() /
0.277)); // converts m/s to kph

                               sb.append(" S" +
round(actualLocation.getSpeed()) + "m "
                                               + kph + "k");
                       }

                       //if (actualLocation.hasBearing() == true)
                       {
                               sb.append(" B" +
round(actualLocation.getBearing()));
                       }

                       sb.append(" T" + new
java.util.Date(actualLocation.getTime())
                                                                       
.toString());

                       sb.append(" " + clock);

                       loc = sb.toString();

               } else {

                       loc = clock;
               }

               return loc;

       }

       public Location getUpdatedGPSLocation() {

               actualLocation =
lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

               return actualLocation;
       }

       public Location getGPSLocation() {
               return actualLocation;
       }


       public long getLocationAge() {
               if (actualLocation != null) {
                       return (System.currentTimeMillis() -
actualLocation.getTime());

               } else {

                       return (0);
               }
       }

       public float getBearing() {
               if ( actualLocation != null &&
actualLocation.hasBearing() == true) {
                       return (actualLocation.getBearing());

               } else {

                       return (-1);
               }
       }

       public float getSpeed() {
               if (actualLocation != null && actualLocation.hasSpeed()
== true) {
                       return (actualLocation.getSpeed());

               } else {

                       return (-1);
               }
       }

       private float round(float toRound) {
               int rounded = (int) ((float) toRound * 10.0f);

               return (((float) rounded) / 10.0f);

       }

   }
}


On Jul 1, 12:14 pm, gjs <[email protected]> wrote:
> Hi there,
>
> You can getspeedin meters per second from the GPS (if present).
>
> see -
>
> http://developer.android.com/reference/android/location/Location.html
>
> Regards
>
> On Jul 1, 1:42 am, ranjan ar <[email protected]> wrote:
>
>
>
> > Hi all,
> > How to determine thespeedof moving android device.any pointers hints are
> > appreciated.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to