• 用户定位


    代码如下:

    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

           }

        }

    }

  • 相关阅读:
    pyCharm专业版下载以及永久破解2099年过期
    win10安装最新的python3解释器
    Ubuntu软件源更换
    Ubuntu安装软件命令
    Target runtime Apache Tomcat 6.0 is not defined
    ajaxFileUpload 报这错jQuery.handleError is not a function
    怎样编写高质量的Java代码
    java 内存监控工具 jstat jmap
    jquery中attr和prop的区别
    javax.mail.MessagingException: 501 Syntax: HELO hostname
  • 原文地址:https://www.cnblogs.com/leihupqrst/p/3722193.html
Copyright © 2020-2023  润新知