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