1, project src Create ... service pack, and then new Gpsservice class
Package Com.zebra.mobilesafe.service;import Java.io.ioexception;import Java.io.inputstream;import Android.app.service;import Android.content.intent;import Android.content.sharedpreferences;import Android.content.sharedpreferences.editor;import Android.location.criteria;import android.location.Location; Import Android.location.locationlistener;import Android.location.locationmanager;import Android.os.Bundle;import Android.os.ibinder;import Android.util.log;public class Gpsservice extends Service {private static final String TAG = "GPS Service ";//Use the Location Services private Locationmanager lm;private Mylocationlistener listener; @Overridepublic IBinder Onbind ( Intent Intent) {//TODO auto-generated method Stubreturn null;} @Overridepublic void OnCreate () {//TODO auto-generated method Stubsuper.oncreate (); LOG.I (TAG, "= = = Enter gps=="); lm = (Locationmanager) getsystemservice (location_service);//List<string> Provider = Lm.getallproviders ();//For (String L:provider) {//System.out.println (L);//}listener= new Mylocationlistener ();//Register Listener location Service//Set conditions for location provider criteria = new criteria (); Criteria.setaccuracy ( Criteria.accuracy_fine);//Set parameter refinement://Criteria.setaccuracy (criteria.accuracy_fine);//set to maximum precision// Criteria.setaltituderequired (false);//not required elevation information//criteria.setbearingrequired (FALSE);//not required azimuth information// Criteria.setcostallowed (TRUE);//whether to allow pay//criteria.setpowerrequirement (Criteria.power_low);//requirements for charge string Proveder = Lm.getbestprovider (criteria, true); Lm.requestlocationupdates (proveder, 0, 0, listener);} @Overridepublic void OnDestroy () {Super.ondestroy ();//Cancel the Listener location Service lm.removeupdates (listener); listener = null;} Class Mylocationlistener implements Locationlistener {/** * callback when position changes */@Overridepublic void onlocationchanged ( Location location) {String longitude = "J:" + location.getlongitude () + "\ n"; String latitude = "W:" + location.getlatitude () + "\ n"; String accuracy = "a" + location.getaccuracy () + "\ n";//Send SMS to security Number//convert standard GPS coordinates to Mars coordinates//inputstream is;//try {//is = Getasse TS (). Open ("Axisoffset.dat");//modifyoffset offset = modifyoffset.getinstance (is);//pointdouble double1 = offset.s2c (new pointdouble (location//. Getlongitude (), Location.getlatitude ()))//longitude = "J:" + offset. x+ "\ n";//latitude = "W:" +offset. Y+ "\ n";////} catch (IOException e) {////TODO auto-generated catch Block//e.printstacktrace ();/} catch (Exception e) {// TODO auto-generated catch Block//e.printstacktrace ();//}sharedpreferences sp = getsharedpreferences ("config", MODE_ PRIVATE); Editor editor = Sp.edit (); Editor.putstring ("Lastlocation", longitude + latitude + accuracy); Editor.commit ();} /** * When the state changes, the callback is turned on-off; off--open/@Overridepublic void onstatuschanged (String provider, int status, Bundle extras) {//T ODO auto-generated Method stub}/** * A location provider can use the */@Overridepublic void onproviderenabled (String provider) {//TODO Auto -generated Method stub}/** * A location provider may not use the */@Overridepublic void onproviderdisabled (String provider) {//TODO Auto-gene Rated method stub}}}
2. Androidmanifest.xml Add Class
Note that this is a service type and is prone to error, i.e. <service android:name= "Com.zebra.mobilesafe.service.GPSService"/>
3. Add Permissions
<uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android: Name= "Android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android:name= " Android.permission.ACCESS_MOCK_LOCATION "/>
4. Start the service according to the operation
Intent i = new Intent (context,gpsservice.class); Context.startservice (i);
Android Get phone Location Code implementation