在androi中GPS信息的獲取可以通過系統提供的LOCATION_SERVICE中的GPS_PROVIDER獲取

 

LocationManager GpsManager  = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
Location        location    = GpsManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);


其中Location 中比較常用的信息有:

 

/*
  location.getAccuracy();    精度 
  location.getAltitude();    高度 : 海拔
  location.getBearing();     導向
  location.getSpeed();       速度
  location.getLatitude();    緯度
  location.getLongitude();   經度
  location.getTime();        UTC時間 以毫秒計
*/

 

GPS信息主要通過注冊回調函數,設定限定條件讓系統服務主動通知,限定條件有以下兩種

1.距離: 當移動距離超過設定值時系統會主動通知 以米記

2.時間:當時間超過設定值時系統會主動通知 以豪秒記

GpsManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, new GpsLocationListener());

這裏設定的是1秒钟,10米为限定條件

 

以下给出具體的實例:

1.在布局文件中增加一個顯示具體信息的文本  (activity_wifi_example.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:gravity="center" >

    <TextView
        android:id="@+id/TextView_GpsInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</LinearLayout>

 

2.在代碼中注冊回調,設定好限制條件,實時刷新GPS狀態信息 (GpsExample.java)

 

package com.example.gpsexample;

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.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class GpsExample extends Activity {

	private final String TAG = "GpsExample";
	
	private TextView        mGpsInfo;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gps_example);

		mGpsInfo      = (TextView)this.findViewById(R.id.TextView_GpsInfo);
		LocationManager GpsManager  = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
		Location        location    = GpsManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

		printGpsLocation(location);
		if( location == null ) { 
			mGpsInfo.setText("暫無GPS有效信息");
		}
		GpsManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, new GpsLocationListener());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_gps_example, menu);
		return true;
	}

	public void printGpsLocation(Location location)
	{
		if( location != null )
		{
			/*
			location.getAccuracy();    精度 
			location.getAltitude();    高度 : 海拔
			location.getBearing();     導向
			location.getSpeed();       速度
			location.getLatitude();    緯度
			location.getLongitude();   經度
			location.getTime();        UTC時間 以毫秒計
			*/
			
			mGpsInfo.setText("Accuracy : " + location.getAccuracy() +
					"\nAltitude : " + location.getAltitude() +
					"\nBearing : " + location.getBearing() +
					"\nSpeed : " + location.getSpeed() +
					"\nLatitude :" + location.getLatitude() +
					"\nLongitude : " + location.getLongitude() + 
					"\nTime : " + location.getTime());
		}
	}
	
	public class GpsLocationListener implements LocationListener
	{
		public void onLocationChanged(Location location) {
			printGpsLocation(location);
		}

		public void onProviderDisabled(String provider) {
			Log.d(TAG, "ProviderDisabled : " + provider);
		}

		public void onProviderEnabled(String provider) {
			Log.d(TAG, "ProviderEnabled : " + provider);
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
			Log.d(TAG, "StatusChanged : " + provider + status);
		}
	}
}


3.最後在AndroidManifest.xml增加獲取GPS權限的支持

 

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




From:CSDN        

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()