SoFunction
Updated on 2025-03-11

Android to obtain the implementation code for mobile phone location

This article shares with you the method of obtaining mobile phone location on Android for your reference. The specific content is as follows

1. Create the...service package under project Src, and then create a new GPSService class

 package ; 
 import ; 
 import ; 
  
 import ; 
 import ; 
 import ; 
 import ; 
 import ; 
 import ; 
 import ; 
 import ; 
 import ; 
 import ; 
 import ; 
  
 public class GPSService extends Service { 
  private static final String TAG = "GPSService"; 
  // Use location services  private LocationManager lm; 
  private MyLocationListener listener; 
  
  @Override 
  public IBinder onBind(Intent intent) { 
   // TODO Auto-generated method stub 
   return null; 
  } 
  
  @Override 
  public void onCreate() { 
   // TODO Auto-generated method stub 
   (); 
   (TAG, "====EnterGPS=="); 
   lm = (LocationManager) getSystemService(LOCATION_SERVICE); 
  
   // List<String> provider = (); 
   // for(String l: provider){ 
   // (l); 
   // } 
   listener = new MyLocationListener(); 
   // Register the listening location service   // Set conditions for location providers   Criteria criteria = new Criteria(); 
   (Criteria.ACCURACY_FINE); 
  
   // Set parameters for refinement:   // (Criteria.ACCURACY_FINE);//Set to maximum accuracy   // (false);//No altitude information required   // (false);//No location information required   // (true);//Is payment allowed   // (Criteria.POWER_LOW);//Requirements for power  
   String proveder = (criteria, true); 
   (proveder, 0, 0, listener); 
  } 
  
  @Override 
  public void onDestroy() { 
   (); 
   // Cancel the listening location service   (listener); 
   listener = null; 
  } 
  
  class MyLocationListener implements LocationListener { 
  
   /**
     * Callback when the position changes
     */ 
  
   @Override 
   public void onLocationChanged(Location location) { 
    String longitude = "j:" + () + "\n"; 
    String latitude = "w:" + () + "\n"; 
    String accuracy = "a" + () + "\n"; 
    // Send a text message to a security number  
    // Convert standard GPS coordinates to Mars coordinates //   InputStream is; 
 //   try { 
 //    is = getAssets().open(""); 
 //    ModifyOffset offset = (is); 
 //    PointDouble double1 = offset.s2c(new PointDouble(location 
 //      .getLongitude(), ())); 
 //    longitude ="j:" + + "\n"; 
 //    latitude = "w:" ++ "\n"; 
 //    
 //   } catch (IOException e) { 
 //    // TODO Auto-generated catch block 
 //    (); 
 //   } catch (Exception e) { 
 //    // TODO Auto-generated catch block 
 //    (); 
 //   } 
  
    SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE); 
    Editor editor = (); 
    ("lastlocation", longitude + latitude + accuracy); 
    (); 
  
   } 
  
   /**
     * Callback when the state changes, on-off; off-off
     */ 
   @Override 
   public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub 
  
   } 
  
   /**
     * Providers can use it in a certain location
     */ 
   @Override 
   public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub 
  
   } 
  
   /**
     * Providers at a certain location are not allowed to use it
     */ 
   @Override 
   public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub 
  
   } 
  
  } 
  
 } 

2. Add a class

NoticeThis is a service type, and it is easy to make errors, that is, <service  android:name=""/>

3. Add permissions

 <uses-permission android:name=".ACCESS_FINE_LOCATION"/> 
  <uses-permission android:name=".ACCESS_COARSE_LOCATION"/> 
 <uses-permission android:name=".ACCESS_MOCK_LOCATION"/> 

4. Start the service according to the operation

 Intent i = new Intent(context,); 
 (i); 

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.