android 搜索Provider
来源:互联网 发布:酷睿第六代处理器知乎 编辑:程序博客网 时间:2024/06/05 07:26
首先在Mainifest.xml获取gps设备的权限
不然的话即使你搜索不到bestProvider
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.lichen" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/></manifest>
布局main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/ScanProvidersButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="测试当前设备的Providers"/> <Button android:id="@+id/bestProviderButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="最好的Provider"/></LinearLayout>
package com.lichen;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; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //位置管理器 locationManager = (LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE); //两个按钮,一个用来搜索Provider,一个用来搜索最好的ProviderscanProvider = (Button) findViewById(R.id.ScanProvidersButton);bestProvider = (Button) findViewById(R.id.bestProviderButton);//遍历Provider,打印到控制台()当中的是一个监听器scanProvider.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// 得到所有的provider,返回的是一个存放String的ListList<String> providers = locationManager.getAllProviders();System.out.println("providers:");for (Iterator<String> it = providers.iterator(); it.hasNext();) {String provider = (String) it.next();System.out.println("=============" + provider);}}});//用来输出最好的ProviderbestProvider.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {Criteria criteria = new Criteria();// 设置查找最好的provider的过滤条件criteria.setAccuracy(Criteria.ACCURACY_FINE);criteria.setSpeedRequired(false);criteria.setCostAllowed(false);// 获得最好的provider,第二个参数表示是否考虑provider可用String provider = locationManager.getBestProvider(criteria,false);System.out.println("best provider ---> " + provider);}}); }}
- android 搜索Provider
- Android Provider
- Android Provider
- android---------provider-----------------
- Android Provider
- Android Provider
- Android Content Provider
- Android Content Provider
- Android Content Provider
- Android Content Provider
- Android Content Provider
- Android中的content provider
- android Content Provider
- Android 复习 Content Provider
- Android Content Provider 测试
- android笔记 Content Provider
- android provider 基础介绍
- Android的Content Provider
- [ java ] java存储区!
- [ java ] java中intern()!
- google笔试大题(2012\10\15)
- 在线视频播放代码大全
- [ java ] 抽象类的小问题!
- android 搜索Provider
- 搞学术研究的第一步,学会找论文
- [ 设计模式 ] 动态代理!
- NYoj 33 蛇形填数
- C++中的vector
- 定点小数原码一位乘(C++实现)
- TCP/IP协议栈与数据包封装(1)
- 堆排序
- CSS3 -webkit-transition(属性渐变)