zoukankan      html  css  js  c++  java
  • 用户定位

    代码如下:

    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

           }

        }

    }

  • 相关阅读:
    理解消息循环和窗口过程(转)
    对话框和控件编程(转)
    俄罗斯方块
    男生女生配(抽屉原理)
    翻转吧,字符串
    数塔
    Pseudoprime numbers伪素数(快速幂+判定素数)
    shǎ崽 OrOrOrOrz
    As Easy As A+B
    求素数(筛选法)
  • 原文地址:https://www.cnblogs.com/leihupqrst/p/3722193.html
Copyright © 2011-2022 走看看