使用基本的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();
}
}
}