Android GPS GPSBasics project hacking

來源:互聯網
上載者:User

標籤:

一、參考源碼:

  GPS Basic Example - Android Example

    http://androidexample.com/GPS_Basic__-__Android_Example/index.php?view=article_discription&aid=68&aaid=93

二、Permission:

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

三、Example:

package com.example.gpsbasics;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.util.Log;import android.widget.Toast;import android.app.Activity;import android.content.Context;public class MainActivity extends Activity implements LocationListener {    private LocationManager locationManager;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        /********** get Gps location service LocationManager object ***********/        /********** 擷取GPS服務管理對象 ************/        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);                /*          Parameters :             First(provider)    :  the name of the provider with which to register                                 :  註冊的名字             Second(minTime)    :  the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.                                 :  最小通知時間間隔,以毫秒為單位。此欄位僅作為節省電力方式,並且位置更新之間的實際時間可以比該值更大或更小。               Third(minDistance) :  the minimum distance interval for notifications, in meters                                 :  最小間隔通知,以毫秒為單位             Fourth(listener)   :  a {#link LocationListener} whose onLocationChanged(Location) method will be called for each location update                                 :  每個位置更新時誰的onLocationChanged (位置)方法將被調用        */                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,                3000,   // 3 sec                10, this);                /********* After registration onLocationChanged method called periodically after each 3 sec ***********/    }        /************* Called after each 3 sec **********/    @Override    public void onLocationChanged(Location location) {                   // location.getLatitude(): 緯度        // location.getLongitude(): 維度        String str = "Latitude: "+location.getLatitude()+" \nLongitude: "+location.getLongitude();        Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();        Log.e("GPSBasics", "onLocationChanged.");    }    @Override    public void onProviderDisabled(String provider) {                /******** Called when User off Gps *********/                Toast.makeText(getBaseContext(), "Gps turned off ", Toast.LENGTH_LONG).show();    }    @Override    public void onProviderEnabled(String provider) {                /******** Called when User on Gps  *********/                Toast.makeText(getBaseContext(), "Gps turned on ", Toast.LENGTH_LONG).show();    }    @Override    public void onStatusChanged(String provider, int status, Bundle extras) {        // TODO Auto-generated method stub            }}

 

Android GPS GPSBasics project hacking

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.