ANDROID_MARS学习笔记_S03_004_getAllProviders、LOCATIONLISTENER、getBestProvider

一、代码

1.xml
(1)activity_main.xml

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

2.java
(1)MainActivity.java

 1 package com.location2;
 2
 3 import java.util.Iterator;
 4 import java.util.List;
 5
 6 import android.app.Activity;
 7 import android.content.Context;
 8 import android.location.Criteria;
 9 import android.location.LocationManager;
10 import android.os.Bundle;
11 import android.view.View;
12 import android.view.View.OnClickListener;
13 import android.widget.Button;
14
15 public class MainActivity extends Activity {
16     private Button scanProvider = null;
17     private Button bestProvider = null;
18     private LocationManager locationManager = null;
19
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24         scanProvider = (Button)findViewById(R.id.ScanProvidersButton);
25         bestProvider = (Button)findViewById(R.id.bestProviderButton);
26         locationManager = (LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE);
27
28         scanProvider.setOnClickListener(new OnClickListener() {
29             @Override
30             public void onClick(View v) {
31                 List<String> providers = locationManager.getAllProviders();
32                 System.out.println("providers----->");
33                 for(Iterator<String> it = providers.iterator() ; it.hasNext() ; ) {
34                     System.out.println(it.next());
35                 }
36             }
37         });
38
39         bestProvider.setOnClickListener(new OnClickListener() {
40             @Override
41             public void onClick(View v) {
42                 Criteria criteria = new Criteria();
43                 criteria.setAccuracy(Criteria.ACCURACY_FINE);
44                 criteria.setPowerRequirement(Criteria.POWER_LOW);
45                 criteria.setAltitudeRequired(false);
46                 criteria.setSpeedRequired(false);
47                 criteria.setCostAllowed(false);
48                 //获得最好的provider,第二个参数表示是否考虑provider可用
49                 String provider = locationManager.getBestProvider(criteria, false);
50                 System.out.println("best provider ---> "+provider);
51             }
52         });
53     }
54 }

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);}});}}

时间: 2024-10-22 16:49:39

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

ANDROID_MARS学习笔记_S03_007_GoogleMap1

一.简介 二.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fi

ANDROID_MARS学习笔记_S04_004_用HTTPCLENT发带参数的get和post请求

一.代码 1.xml(1)activity_main.xml 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="

ANDROID_MARS学习笔记_S02_012_ANIMATION_利用AnimationListener在动画结束时删除或添加组件

一.代码 1.xml(1)activity_main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/layoutId" 4 android:orientation="

ANDROID_MARS学习笔记_S01原始版_013_广播机制二

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_pa

ANDROID_MARS学习笔记_S01原始版_012_广播机制一

一.简介 二.代码1.xml(1)activity_main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width

ANDROID_MARS学习笔记_S01原始版_007_Handler及线程的简单使用

一.运行结果 一.代码1.xml(1)activity_main.xml 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_heig

ANDROID_MARS学习笔记_S01原始版_008_Handler(异步消息处理机制)

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_pa

ANDROID_MARS学习笔记_S02_004_ExpandableListActivity

1.main.xml 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"

ANDROID_MARS学习笔记_S02_005_AppWidget1

一.AppWidget介绍 1.Widget的定义创建流程 (1)在res新建xml文件夹,新建appwidget-provider.xml,为widget提供元数据 (2)在res/layout文件夹下定义widget的样式 (3)在src新建类,extends AppWidgetProvider,自定义widget (4)在AndroidManifest.xml中把自定义的widget添加为receiver,接收APPWIDGET_UPDATE广播 2. 二.代码 1.res/xml/exa