天天看點

Android-GPS定位

使用基本的GPS定位功能大概需要一下一個步驟:

1.        擷取定位的執行個體

2.        設定一個更新的時間周期和最小距離變化

3.        設定監聽器LocationListener

4.        在監聽器中編寫當GPS發生不同變化時相應的動作

5.        在配置檔案AndroidMainfes.xml中加入權限

<uses-permissionandroid:name="android.permission.INTERNET"/><!--通路網絡-->

<uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/><!--擷取粗略的位置-->

<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/><!--擷取精确的位置-->

6.        打開GPS,開始定位

package com.example.gps;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.TextView;

public class GPSActivity extends Activity {
	private LocationManager locamanManager;
	private TextView txtvieWarn;
	private TextView txtvieState;
	private Button btnOpenGps;
	/*
	 * 1.擷取定位的執行個體
	 * 2.設定一個更新的時間周期和最小距離變化
	 * 3.設定監聽器LocationListener
	 * 4.在監聽器中編寫當GPS發生不同變化時相應的動作
	 * 5.在配置檔案AndroidMainfest.xml中加入權限
	 	*   <uses-permission android:name="android.permission.INTERNET"/> <!--通路網絡-->
	 	*   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <!--擷取粗略的位置-->
	 	*   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <!--擷取精确的位置-->
	 * 6.打開GPS,開始定位
	*/
	public void UpdateView(Location location)
	{
		StringBuffer strbufTemp = new StringBuffer();
		if (location == null)
		{
			txtvieWarn.setText("未獲得服務");
			return;
		}
		strbufTemp.append("經度" + location.getLongitude() + "\n");
		strbufTemp.append("緯度" + location.getLatitude() + "\n");
	//	strbufTemp.append("高度" + locaPlace.getAltitude() + "\n");
	//	strbufTemp.append("速度" + locaPlace.getSpeed() + "\n");
	//	strbufTemp.append("方向" + locaPlace.getBearing() + "\n");
		txtvieWarn.setText(strbufTemp.toString());
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gps);
		
		txtvieWarn = (TextView)this.findViewById(R.id.txtvieInfor);
		txtvieState = (TextView)this.findViewById(R.id.txtvieState);
		btnOpenGps = (Button)this.findViewById(R.id.btnOpenGPS);
		//設定監聽器,按下按鈕時打開GPS
		btnOpenGps.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				openGPS(GPSActivity.this);
			}
			
		});
		/*
		 * 1.通過getSystemService擷取一個位置服務
		 * 2.設定位置更新的周期為1s,最小距離為0.0001米,監聽器為locationListener
		 */
		locamanManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		locamanManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0.0001f, locationListener);
	//	Location location = locamanManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);		
	}
	/*
	 * 當GPS發生變化時,根據不同變法作出相應的動作
	 */
	private final LocationListener locationListener = new LocationListener(){
		public void onLocationChanged(Location location){
		//	Location location = locamanManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		/*	Criteria criteriaGps = new Criteria();
			criteriaGps.setAccuracy(Criteria.ACCURACY_FINE);//細經度要求
			criteriaGps.setAltitudeRequired(false);//不需要高度資訊
			criteriaGps.setBearingRequired(false);//不需要軸承資訊
			criteriaGps.setCostAllowed(false);//允許造成貨币成本
			criteriaGps.setPowerRequirement(Criteria.POWER_LOW);//低功耗*/
			txtvieState.setText("位置變化" + location.getLongitude());
			UpdateView(location);
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			txtvieState.setText("切換Gps狀态");
		}

		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			txtvieState.setText("開啟Gps");
			Location location = locamanManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			UpdateView(location);
		}

		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			txtvieState.setText("關閉Gps");
		}
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.g, menu);
		return true;
	}
	/*強制打開GPS服務*/
	public static final void openGPS(Context context) 
	{
        Intent GPSIntent = new Intent(); 
        GPSIntent.setClassName("com.android.settings", 
                "com.android.settings.widget.SettingsAppWidgetProvider"); 
        GPSIntent.addCategory("android.intent.category.ALTERNATIVE"); 
        GPSIntent.setData(Uri.parse("custom:3")); 
        try 
        { 
             PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send(); 
        } 
        catch (CanceledException e)
        { 
             e.printStackTrace(); 
        } 
    }
}