ANDROID_MARS学习笔记_S03_004_getAllProvidersLOCATIONLISTENERgetBestProvider

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ANDROID_MARS学习笔记_S03_004_getAllProvidersLOCATIONLISTENERgetBestProvider相关的知识,希望对你有一定的参考价值。

、代码

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

 

以上是关于ANDROID_MARS学习笔记_S03_004_getAllProvidersLOCATIONLISTENERgetBestProvider的主要内容,如果未能解决你的问题,请参考以下文章

ANDROID_MARS学习笔记_S01原始版_004_TableLayout

ANDROID_MARS学习笔记_S05_004_过滤杂质,得到真正的加速度

ANDROID_MARS学习笔记_S01_004dpidp(dip)及计算

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

ANDROID_MARS学习笔记_S03_001_获取蓝牙匹配列表

ANDROID_MARS学习笔记_S03_007_GoogleMap1