一、代码

1.xml
(1)activity_main.xml

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

2.java
(1)MainActivity.java

 package com.location2;

 import java.util.Iterator;
import java.util.List; import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
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 scanProvider = null;
private Button bestProvider = null;
private LocationManager locationManager = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scanProvider = (Button)findViewById(R.id.ScanProvidersButton);
bestProvider = (Button)findViewById(R.id.bestProviderButton);
locationManager = (LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE); scanProvider.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
List<String> providers = locationManager.getAllProviders();
System.out.println("providers----->");
for(Iterator<String> it = providers.iterator() ; it.hasNext() ; ) {
System.out.println(it.next());
}
}
}); bestProvider.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(false);
//获得最好的provider,第二个参数表示是否考虑provider可用
String provider = locationManager.getBestProvider(criteria, false);
System.out.println("best provider ---> "+provider);
}
});
}
}

package com.location2;
import java.util.Iterator;import java.util.List;
import android.app.Activity;import android.content.Context;import android.location.Criteria;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 scanProvider = null;private Button bestProvider = null;private LocationManager locationManager = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);        scanProvider = (Button)findViewById(R.id.ScanProvidersButton);        bestProvider = (Button)findViewById(R.id.bestProviderButton);        locationManager = (LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE);                scanProvider.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {List<String> providers = locationManager.getAllProviders();System.out.println("providers----->");for(Iterator<String> it = providers.iterator() ; it.hasNext() ; ) {System.out.println(it.next());}}});                bestProvider.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Criteria criteria = new Criteria();criteria.setAccuracy(Criteria.ACCURACY_FINE);criteria.setPowerRequirement(Criteria.POWER_LOW);criteria.setAltitudeRequired(false);criteria.setSpeedRequired(false);criteria.setCostAllowed(false);//获得最好的provider,第二个参数表示是否考虑provider可用String provider = locationManager.getBestProvider(criteria, false);System.out.println("best provider ---> "+provider);}});}}

ANDROID_MARS学习笔记_S03_004_getAllProviders、LOCATIONLISTENER、getBestProvider的更多相关文章

  1. ANDROID_MARS学习笔记_S03_003_LocationManager、LocationListener

    一.简介 二.代码1.xml (1)AndroidManifest.xml 增加 <uses-permission android:name="android.permission.A ...

  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. Browser 对象

    Browser 对象 window对象表示浏览器中打开的窗口如果文档包含框架(iframe 或 iframe标签),浏览器会为HTML文档创建一个window对象,并为每个框架创建一个额外的windo ...

  2. 1066. Root of AVL Tree

    An AVL tree is a self-balancing binary search tree.  In an AVL tree, the heights of the two child su ...

  3. 我也来学着写写WINDOWS服务-解析xml抓取数据并插入数据库

    项目告一段落,快到一年时间开发了两个系统,一个客户已经在试用,一个进入客户测试阶段,中间突然被项目经理(更喜欢叫他W工)分派一个每隔两小时用windows服务去抓取客户提供的外网xml,解析该xml, ...

  4. [02] Oracle简单单行函数(字符+数值+日期+转换+通用)

    1. 字符函数 --upper(str), lower(str):将str转换成大,小写 select upper('hello') as x1, lower('HELlo') as x2 from ...

  5. UIImagePickerController显示中文界面

    iOS开发中,我们经常遇到获取拍照.相册中图片的功能,就必然少不了UIImagePickerController,但是我们发现当我们使用它的时候,它的页面是英文的,看着很别扭,国人还是比较喜欢看中文界 ...

  6. java新手笔记1 Hello World!

    //Hello.java文件 //类声明 public class Hello{ //声明方法 main程序入口 public static void main (String[] args) { S ...

  7. MongoDB源码编译

    MongoDB源码编译 本人编译的版本编译的版本为mongodb2.6分支,目前MongoDB3.0已经发布,编译步骤和2.6的差不多,不过3.0版本要求编译器支持c++11标准,所以如果是在Linu ...

  8. Object-API-NSLog

    NSLog中的基础数据类型 输出格式: NSLog("") char: %c short int: %hi %hx %ho unsigned short int: %hu %hx ...

  9. O_NONBLOCK模式下写fifo的注意事项

    后台网络通信框架一般采用fifo来作为事件通知的机制:创建一个fifo,然后以非阻塞读和非阻塞写的方式打开fifo,然后把fd加到epoll里面,作为通知网络事件的fd. 在这里有个隐晦的问题容易被忽 ...

  10. 【转】oracle null

    转自:oracle的null和空字符串'' 1.oracle 将 空字符串即''当成null 2.null 与任何值做逻辑运算得结果都为 false,包括和null本身 3.用 is null 判断时 ...