Android access to mobile phone location Code _android

Source: Internet
Author: User
Tags stub

This example for you to share the Android access to mobile phone location for your reference, the specific content as follows

1, under the project SRC Create ... service pack, 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 = "Gpsservice"; 
  Use to position service private Locationmanager LM; 
  
  Private Mylocationlistener listener; 
  @Override public IBinder onbind (Intent Intent) {//TODO auto-generated the method stub return null; 
   @Override public void OnCreate () {//TODO auto-generated Method Stub super.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 (); 
   Registering a listening location service//setting criteria for a location Provider = new criteria (); 
  
   Criteria.setaccuracy (Criteria.accuracy_fine); Set parameter refinement://Criteria.setaccuracy (criteria.accuracy_fine);//set to maximum precision//criteria.setaltituderequired (FALSE);//Do not require sea Pull information//criteria.setbearingrequired (FALSE);//do not require azimuth information//criteria.setcostallowed (TRUE);//whether to allow payment//CRITERIA.SETP 
   Owerrequirement (Criteria.power_low)//Power requirements String Proveder = Lm.getbestprovider (Criteria, true); 
  Lm.requestlocationupdates (proveder, 0, 0, listener); 
   @Override public void OnDestroy () {Super.ondestroy (); 
   Cancel the Listening position service lm.removeupdates (listener); 
  listener = NULL; Class Mylocationlistener implements Locationlistener {/** * when position changes, callback/@Override p ublic 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 = Getassets (). 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 opens--closes, closes--opens/@Override public void onstatuschanged (String provider, int Status, Bundle Extras) {//TODO auto-generated Method stub}/** * A location provider can use the */@Ov 
    Erride public void onproviderenabled (String provider) {//TODO auto-generated method stub}/** * A location provider may not use the/@Override public void onproviderdisabled (String provider) {//TODO auto-generate  D Method Stub}}

2, Androidmanifest.xml Add Class

Note that this is the service type, it is easy to make mistakes, namely <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); 
 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.