【代碼】android 通過GPS擷取使用者地理位置並監聽位置變化

來源:互聯網
上載者:User
LocationActivity.java/*  LocationActivity.java *  @author octobershiner *  2011 7 22 *  SE.HIT *  一個示範定位使用者的位置並且監聽位置變化的代碼 * */package uni.location;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.os.Vibrator;import android.util.Log;import android.widget.TextView;public class LocationActivity extends Activity {    /** Called when the activity is first created. *///建立lcoationManager對象private LocationManager manager;private static final String TAG = "LOCATION DEMO";    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //擷取系統的服務,        manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);        Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);        //第一次獲得裝置的位置        updateLocation(location);           //重要函數,監聽資料測試        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 10,                          locationListener);                              }    /*此處更新一下,當activity不處於活動狀態時取消GPS的監聽*/      public void onPause(){       super.onPause();       locationManager.removeListener(locationListener);}    //建立一個事件監聽器   private final LocationListener locationListener = new LocationListener() {               public void onLocationChanged(Location location) {               updateLocation(location);               }               public void onProviderDisabled(String provider){            updateLocation(null);               Log.i(TAG, "Provider now is disabled..");            }               public void onProviderEnabled(String provider){            Log.i(TAG, "Provider now is enabled..");            }               public void onStatusChanged(String provider, int status,Bundle extras){ }       };       //擷取使用者位置的函數,利用Log顯示    private void updateLocation(Location location) {               String latLng;                if (location != null) {              double lat = location.getLatitude();               double lng = location.getLongitude();                        latLng = "Latitude:" + lat + "  Longitude:" + lng;               } else {               latLng = "Can't access your location";             }            Log.i(TAG, "The location has changed..");            Log.i(TAG, "Your Location:" +latLng);    }    }

只修改activity檔案是不夠的,因為android系統的安全性,對服務採用授權的機制,所以需要修改manifest.xml檔案

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="uni.location"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="8" />    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".LocationActivity"                  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-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> </manifest>

很多朋友可能會有疑問,那就是GPS定位在android虛擬機器上的調試問題,其實是可以類比的,大家啟動虛擬機器,然後開啟DDMS的介面,左側device欄目會動態顯示虛擬機器上各項服務啟動的情況,待出虛擬機器現解鎖介面後,單機device欄目下面的emulator行,這時會發現下面的emulator control下面會有 location control ,開啟裡面的manual標籤,哈哈相信你已經看到了經緯度,你可以更改。運行你的程式,然後單擊剛才經緯度設定的send按鈕就可以類比接受到新的地理位置了。

在這個demo中 我用到了Log顯示狀態,推薦使用這種方法,很好用,想瞭解的朋友可以參考一下我的另一篇文章,學會巧妙的使用Log,跟推薦大家搜一下sundyzlh的教學視頻。

 

關於LOG的使用
單擊連結

最終的效果

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.