1. 主程序文件

package com.ex06.button;import android.app.Activity;import android.content.Context;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class ActivityButton extends Activity {      /** Called when the activity is first created. */      LocationManager locationManager;       private EditText editText;    Button btnGetLocation;        @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);                     btnGetLocation=(Button)findViewById(R.id.button1);          editText = (EditText)findViewById(R.id.speed);        editText.setText("Location:\n");        btnGetLocation.setOnClickListener(new bntOnClickListen());                    //通过getSystemService接口获取LocationManager实例          locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);                    //实现监听器 LocationListener           LocationListener locationlisten=new LocationListener() {                            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {                  // TODO Auto-generated method stub                  // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数              }                            public void onProviderEnabled(String arg0) {                  // TODO Auto-generated method stub                  // Provider被enable时触发此函数,比如GPS被打开              }                            public void onProviderDisabled(String arg0) {                  // TODO Auto-generated method stub                  // Provider被disable时触发此函数,比如GPS被关闭              }                            //当坐标改变时触发此函数;如果Provider传进相同的坐标,它就不会被触发              @Override            public void onLocationChanged(Location arg0) {                  // TODO Auto-generated method stub                  if (arg0 != null) {                         Log.i("log", "Location changed : Lat: "  + arg0.getLatitude() + " Lng: " + arg0.getLongitude());                     editText.setText(arg0.getLatitude() + " Lng: " + arg0.getLongitude() + "\n");                } else {                Log.i("log", "Location changed : Lat: "  + "NULL" + " Lng: " + "NULL");                  editText.setText("NULL" + " Lng: " + "NULL" + "\n");                }            }        };                    // 注册监听器 locationListener           //第 2 、 3个参数可以控制接收GPS消息的频度以节省电力。第 2个参数为毫秒, 表示调用 listener的周期,第 3个参数为米 ,表示位置移动指定距离后就调用 listener          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationlisten);                 }            class bntOnClickListen implements OnClickListener{      @Override        public void onClick(View v) {              // TODO Auto-generated method stub              //获取地理位置信息数据(如果没实现监听器和注册监听器,好像获取不了地理数据)              Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);              if (location!=null) {                  String strLati = Double.toString(location.getLatitude());                  String strLong = Double.toString(location.getLongitude());                  //显示地理位置数据                  System.out.println("---------地理位置信息---------");                  System.out.println("---------" + strLati + "/" + strLong + "---------");                  editText.setText(strLati + "/" + strLong + "\n");            }              else{                  Log.i("log", "location==NULL");                editText.setText("location==NULL");            }          }              }  }  

2. 添加权限

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


更多相关文章

  1. [Android]在Avtivity中注册监听器
  2. android之检查service运行状态函数
  3. Android Studio 基础 之 一键快速实现一个类的 构造函数、getter
  4. Android Studio如何提示函数用法
  5. Android Studio怎样提示函数使用方法
  6. Android NDK编程实现终端功能(调用system函数)
  7. 在Android中使用HTML5本地存储,地理位置,离线应用
  8. 使用Android提供的模拟任意地理位置,报java.lang.IllegalArgument
  9. 浅谈android系统java调用C++函数传参过程

随机推荐

  1. Android(安卓)api level对照表
  2. JavaEE还是Android?
  3. 2018 Android 框架汇总(转)
  4. Android 加速度传感器 (G-Sensor) 收
  5. android之Menu 实例与详解
  6. maven 学习笔记(二)-创建简单的eclipse+and
  7. android菜鸟学习笔记15----Android Junit
  8. android 4.4.3 css hack 写法
  9. AndroidStudio-Eat-Guide—— 1.下载安装
  10. 通过Titanium Studio为Android APK签名