import android.app.Activity;
1. import android.content.Context;
2. import android.location.Criteria;
3. import android.location.Location;
4. import android.location.LocationListener;
5. import android.location.LocationManager;
6. import android.os.Bundle;
7. import android.widget.TextView;
8.
9. public class CurrentLocation extends Activity {
10. /** Called when the activity is first created. */
11. @Override
12. public void onCreate(Bundle savedInstanceState) {
13. super.onCreate(savedInstanceState);
14. setContentView(R.layout.main);
15. LocationManager locationManager;
16. String serviceName = Context.LOCATION_SERVICE;
17. locationManager = (LocationManager)getSystemService(serviceName);
18. //String provider = LocationManager.GPS_PROVIDER;
19.
20. Criteria criteria = new Criteria();
21. criteria.setAccuracy(Criteria.ACCURACY_FINE);
22. criteria.setAltitudeRequired(false);
23. criteria.setBearingRequired(false);
24. criteria.setCostAllowed(true);
25. criteria.setPowerRequirement(Criteria.POWER_LOW);
26. String provider = locationManager.getBestProvider(criteria, true);
27.
28. Location location = locationManager.getLastKnownLocation(provider);
29. updateWithNewLocation(location);
30. locationManager.requestLocationUpdates(provider, 2000, 10,
31. locationListener);
32. }
33. private final LocationListener locationListener = new LocationListener() {
34. public void onLocationChanged(Location location) {
35. updateWithNewLocation(location);
36. }
37. public void onProviderDisabled(String provider){
38. updateWithNewLocation(null);
39. }
40. public void onProviderEnabled(String provider){ }
41. public void onStatusChanged(String provider, int status,
42. Bundle extras){ }
43. };
44. private void updateWithNewLocation(Location location) {
45. String latLongString;
46. TextView myLocationText;
47. myLocationText = (TextView)findViewById(R.id.myLocationText);
48. if (location != null) {
49. double lat = location.getLatitude();
50. double lng = location.getLongitude();
51. latLongString = "纬度:" + lat + "\n经度:" + lng;
52. } else {
53. latLongString = "无法获取地理信息";
54. }
55. myLocationText.setText("您当前的位置是:\n" +
56. latLongString);
57. }
58. }
同时要添加
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses- permission>
权限
通过以上程序可以获取系统的GPS信息,由于系统可以设置是否开启GPS功能,如果用户关闭了此功能程序是无法获取GPS信息的,我们可以通过下面的代码检测他是否开启,如果没有开启可以提示用户打开此功能。
private void openGPSSettings()
{
LocationManager alm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
if (alm
.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT)
.show();
return;
}
Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent,0); //此为设置完成后返回到获取界面
}
我们如果在模拟器测试GPS功能时需要我们去手动的给GPS模块赋值。具体操作是:
在“开始”菜单下先“运行”,输入 “CMD”,然后再输入 telnet localhost 5554,回车转接模拟器,然后再输入 geo fix x y就可以了,x Y 就是输入的经纬度。
向模拟器GPS赋值时要将模拟器的时间调成正确的时间,也就是同电脑的时间是一致的,否则会接收不到。
在多个程序同时请求GPS信息的时候现在还没有发现会出现不可以同时复用的现象。