Android获取手机位置代码实现

1、项目Src下创建...service包,然后新建GPSService类

  1. package com.zebra.mobilesafe.service;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import android.app.Service;
  5. import android.content.Intent;
  6. import android.content.SharedPreferences;
  7. import android.content.SharedPreferences.Editor;
  8. import android.location.Criteria;
  9. import android.location.Location;
  10. import android.location.LocationListener;
  11. import android.location.LocationManager;
  12. import android.os.Bundle;
  13. import android.os.IBinder;
  14. import android.util.Log;
  15. public class GPSService extends Service {
  16. private static final String TAG = 'GPSService';
  17. // 用到位置服务
  18. private LocationManager lm;
  19. private MyLocationListener listener;
  20. @Override
  21. public IBinder onBind(Intent intent) {
  22. // TODO Auto-generated method stub
  23. return null;
  24. }
  25. @Override
  26. public void onCreate() {
  27. // TODO Auto-generated method stub
  28. super.onCreate();
  29. Log.i(TAG, '====进入GPS==');
  30. lm = (LocationManager) getSystemService(LOCATION_SERVICE);
  31. // List<String> provider = lm.getAllProviders();
  32. // for(String l: provider){
  33. // System.out.println(l);
  34. // }
  35. listener = new MyLocationListener();
  36. // 注册监听位置服务
  37. // 给位置提供者设置条件
  38. Criteria criteria = new Criteria();
  39. criteria.setAccuracy(Criteria.ACCURACY_FINE);
  40. // 设置参数细化:
  41. // criteria.setAccuracy(Criteria.ACCURACY_FINE);//设置为最大精度
  42. // criteria.setAltitudeRequired(false);//不要求海拔信息
  43. // criteria.setBearingRequired(false);//不要求方位信息
  44. // criteria.setCostAllowed(true);//是否允许付费
  45. // criteria.setPowerRequirement(Criteria.POWER_LOW);//对电量的要求
  46. String proveder = lm.getBestProvider(criteria, true);
  47. lm.requestLocationUpdates(proveder, 0, 0, listener);
  48. }
  49. @Override
  50. public void onDestroy() {
  51. super.onDestroy();
  52. // 取消监听位置服务
  53. lm.removeUpdates(listener);
  54. listener = null;
  55. }
  56. class MyLocationListener implements LocationListener {
  57. /**
  58. * 当位置改变的时候回调
  59. */
  60. @Override
  61. public void onLocationChanged(Location location) {
  62. String longitude = 'j:' + location.getLongitude() + '\n';
  63. String latitude = 'w:' + location.getLatitude() + '\n';
  64. String accuracy = 'a' + location.getAccuracy() + '\n';
  65. // 发短信给安全号码
  66. // 把标准的GPS坐标转换成火星坐标
  67. //InputStream is;
  68. //try {
  69. //is = getAssets().open('axisoffset.dat');
  70. //ModifyOffset offset = ModifyOffset.getInstance(is);
  71. //PointDouble double1 = offset.s2c(new PointDouble(location
  72. //.getLongitude(), location.getLatitude()));
  73. //longitude ='j:' + offset.X+ '\n';
  74. //latitude = 'w:' +offset.Y+ '\n';
  75. //
  76. //} catch (IOException e) {
  77. //// TODO Auto-generated catch block
  78. //e.printStackTrace();
  79. //} catch (Exception e) {
  80. //// TODO Auto-generated catch block
  81. //e.printStackTrace();
  82. //}
  83. SharedPreferences sp = getSharedPreferences('config', MODE_PRIVATE);
  84. Editor editor = sp.edit();
  85. editor.putString('lastlocation', longitude + latitude + accuracy);
  86. editor.commit();
  87. }
  88. /**
  89. * 当状态发生改变的时候回调 开启--关闭 ;关闭--开启
  90. */
  91. @Override
  92. public void onStatusChanged(String provider, int status, Bundle extras) {
  93. // TODO Auto-generated method stub
  94. }
  95. /**
  96. * 某一个位置提供者可以使用了
  97. */
  98. @Override
  99. public void onProviderEnabled(String provider) {
  100. // TODO Auto-generated method stub
  101. }
  102. /**
  103. * 某一个位置提供者不可以使用了
  104. */
  105. @Override
  106. public void onProviderDisabled(String provider) {
  107. // TODO Auto-generated method stub
  108. }
  109. }
  110. }

2、AndroidManiFest.xml添加类

注意这个是service类型,很容易出错,即<service  android:name='com.zebra.mobilesafe.service.GPSService'/>

3、添加权限

  1. <uses-permission android:name='android.permission.ACCESS_FINE_LOCATION'/>
  2. <uses-permission android:name='android.permission.ACCESS_COARSE_LOCATION'/>
  3. <uses-permission android:name='android.permission.ACCESS_MOCK_LOCATION'/>

4、根据操作启动服务

  1. Intent i = new Intent(context,GPSService.class);
  2. context.startService(i);
(0)

相关推荐