一、简介

二、代码
1.xml

(1)AndroidManifest.xml

增加

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

2.java
(1)MainActivity.java

 package com.location1;

 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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { private Button button = null;
private LocationManager locationManager = null;
LocationListener locationListener = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.buttonId);
locationManager = (LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE); locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
System.out.println("onStatusChanged--->");
} @Override
public void onProviderEnabled(String provider) {
System.out.println("onProviderEnabled--->");
} @Override
public void onProviderDisabled(String provider) {
System.out.println("onProviderDisabled--->");
} @Override
public void onLocationChanged(Location location) {
System.out.println("onLocationChanged--->");
System.out.println(location.getLatitude());
System.out.println(location.getLongitude());
}
}; button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//绑定监听器,第二个参数表示更新的最小时间(毫秒);第三个参数表示更新的最小距离(米)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
});
}
}

ANDROID_MARS学习笔记_S03_003_LocationManager、LocationListener的更多相关文章

  1. ANDROID_MARS学习笔记_S03_004_getAllProviders、LOCATIONLISTENER、getBestProvider

    一.代码 1.xml(1)activity_main.xml <uses-permission android:name="android.permission.ACCESS_FINE ...

  2. ANDROID_MARS学习笔记_S01_012_RatingBar

    1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  3. ANDROID_MARS学习笔记_S01_012_SeekBar

    1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  4. ANDROID_MARS学习笔记_S01_011ProgressBar

    文档是这样来设置样式 <ProgressBar android:layout_width="wrap_content" android:layout_height=" ...

  5. ANDROID_MARS学习笔记_S01_010日期时间控件

    1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  6. ANDROID_MARS学习笔记_S01_009Relative_LAYOUT例子

    1. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android ...

  7. ANDROID_MARS学习笔记_S01_008Linear_layout例子

    1.netstone_layout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  8. ANDROID_MARS学习笔记_S01_007Linear_layout嵌套与layout_weight的设置

    一.介绍 二.1.linear_layout.xml <?xml version="1.0" encoding="utf-8"?> <Line ...

  9. ANDROID_MARS学习笔记_S01_006ImageView

    一.ImageView介绍 设置scalType Must be one of the following constant values. Constant Value Description ma ...

随机推荐

  1. 不同浏览器应用scrollTop属性

    window.onscroll = _onScroll;function _onScroll(){ var aside = document.getElementsByClassName('aside ...

  2. SQL Server数据的导入导出

    SQL Server 2008的导入导出服务可以实现不同类型的数据库系统的数据转换.为了让用户可以更直观的使用导入导出服务,微软提供了导入导出向导.导入和导出向导提供了一种从源向目标复制数据的最简便的 ...

  3. SQLSERVER一些公用DLL的作用解释

    如果你的SQLSERVER安装在C盘的话,下面的路径就是相应SQLSERVER版本的公用DLL的存放路径 SQL2005 C:\Program Files\Microsoft SQL Server\9 ...

  4. 学习笔记7_Java_day11_JSP原理(5)

    4. jsp原理(理解) * jsp其实是一种特殊的Servlet > 当jsp页面第一次被访问时,服务器会把jsp编译成java文件(这个java其实是一个servlet类) > 然后再 ...

  5. Haproxy配置参数

    HAProxy配置中分成五部分内容,当然这些组件不是必选的,可以根据需要选择部分作为配置. ===================== global    参数是进程级的,通常和操作系统(OS)相关. ...

  6. Objective-C MRC多个对象相互引用的内存管理

    在MRC环境下,假定CTRoom对象是CTPerson的一个成员变量,那么修改CTRoom对象时应注意,代码如下: - (void) setRoom:(CTRoom *) room { //需判断新旧 ...

  7. IOS-开发日志-UILabel相关

    UILabel属性 1.text:设置标签显示文本. 2.attributedText:设置标签属性文本. Ios代码 NSString *text = @"first"; NSM ...

  8. (转)Xcode 中设置部分文件ARC支持

    ARC是什么 ARC是iOS 5推出的新功能,全称叫 ARC(Automatic Reference Counting).简单地说,就是代码中自动加入了retain/release,原先需要手动添加的 ...

  9. ubuntu intelliJ IDEA 12.1.4 安装

    1 php插件 http://plugins.jetbrains.com/plugin/?id=6610 把插件下载到一个目录下,如果插件不兼容,多试几个版本! 2 打开ide, FILE -> ...

  10. C#面向对象的学习笔记

    1.面向对象的3要素: 封装:将不需要显示的代码封装到一个方法中,只对外提供方法名,用户不需关心内部实现. 继承:子类继承父类,公用父类的代码,大大提高了代码的重用,贴近生活也符合人类的编程思想. 多 ...