SoFunction
Updated on 2025-04-10

Case code for Android to implement mobile phone positioning

Android phone positioning case code

The code is as follows:

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
  * Mobile phone positioning program code
  * @author xuliugen
  */
public class MainActivity extends Activity {
  // Use location services  private LocationManager lm;
  private MyLocationListener listener;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    // How to get positioning    // 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
  protected void onDestroy() {
    // TODO Auto-generated method stub
    ();
    // Cancel the listening location service    (listener);
    listener = null;
  }
  class MyLocationListener implements LocationListener {
    /**
      * Callback when the position changes
      */
    public void onLocationChanged(Location location) {
      String longitude = "longitude:" + ();
      String latitude = "latitude:" + ();
      String accuracy = "Accuracy:" + ();
      TextView textview = new TextView();
      (longitude + "\n" + latitude + "\n" + accuracy);
      setContentView(textview);
    }
    /**
      * Callback when the state changes, on-off; off-off
      */
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
    /**
      * Providers can use it in a certain location
      */
    public void onProviderEnabled(String provider) {
    }
    /**
      * Providers at a certain location are not allowed to use it
      */
    public void onProviderDisabled(String provider) {
    }
  }
}

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the relevant links below