• 用户定位


    代码如下:

    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

           }

        }

    }

  • 相关阅读:
    覆盖索引和联合索引
    docker是个啥?
    golang--解决邮件发送标题乱码问题
    爬虫-爬取美少女壁纸
    漫谈--ssh协议-中间人攻击
    golang--常用的字符串操作
    干支纪年
    JS实现纯前端将数据导出Excel两种方式亲测有效
    猴子吃香蕉-Java岗位面试题
    content:"26A1"特殊字符和图标记录
  • 原文地址:https://www.cnblogs.com/leihupqrst/p/3722193.html
Copyright © 2020-2023  润新知