用户定位

代码如下:

AndroidManifest.xml:

在</application>后面增加一行:

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

Activity_main.xml:

    <Button

       android:id="@+id/locationButtonId"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

        android:text="绑定监听器"/>

MainActivity.java:

package leihu.location01;

import android.location.Location;

import android.location.LocationListener;

import android.location.LocationManager;

import android.os.Bundle;

import android.app.Activity;

import android.content.Context;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MainActivity extends Activity {

    private Button button = null;

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

       button = (Button)findViewById(R.id.locationButtonId);

       button.setOnClickListener(new ButtonListener());

    }

    private class ButtonListener implements OnClickListener{

       public void onClick(View v) {

           //得到LocationManager对象

           LocationManager locationManager = (LocationManager)MainActivity.this.getSystemService(Context.LOCATION_SERVICE);

           //1.定义当前所使用的Location Provider     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,newTestLocationListener());

       }

    }

    private class TestLocationListener implements LocationListener{

       public void onLocationChanged(Location location) {

           // TODO Auto-generated method stub

           System.out.println(location.getLongitude());

           System.out.println(location.getLatitude());

       }

       public void onProviderDisabled(String provider) {

           // TODO Auto-generated method stub

        }

       public void onProviderEnabled(String provider) {

           // TODO Auto-generated method stub

       }

       public void onStatusChanged(String provider, int status, Bundle extras) {

           // TODO Auto-generated method stub

       }

    }

}

原文地址:https://www.cnblogs.com/leihupqrst/p/3722193.html