當前位置: 首頁>>代碼示例>>Java>>正文


Java LocationManager.getBestProvider方法代碼示例

本文整理匯總了Java中android.location.LocationManager.getBestProvider方法的典型用法代碼示例。如果您正苦於以下問題:Java LocationManager.getBestProvider方法的具體用法?Java LocationManager.getBestProvider怎麽用?Java LocationManager.getBestProvider使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在android.location.LocationManager的用法示例。


在下文中一共展示了LocationManager.getBestProvider方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: register

import android.location.LocationManager; //導入方法依賴的package包/類
/**
 * 注冊
 * <p>使用完記得調用{@link #unregister()}</p>
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>}</p>
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>}</p>
 * <p>如果{@code minDistance}為0,則通過{@code minTime}來定時更新;</p>
 * <p>{@code minDistance}不為0,則以{@code minDistance}為準;</p>
 * <p>兩者都為0,則隨時刷新。</p>
 *
 * @param minTime     位置信息更新周期(單位:毫秒)
 * @param minDistance 位置變化最小距離:當位置距離變化超過此值時,將更新位置信息(單位:米)
 * @param listener    位置刷新的回調接口
 * @return {@code true}: 初始化成功<br>{@code false}: 初始化失敗
 */
public static boolean register(long minTime, long minDistance, OnLocationChangeListener listener) {
    if (listener == null) return false;
    mLocationManager = (LocationManager) Utils.getContext().getSystemService(Context.LOCATION_SERVICE);
    mListener = listener;
    if (!isLocationEnabled()) {
        ToastUtils.showShortToastSafe("無法定位,請打開定位服務");
        return false;
    }
    String provider = mLocationManager.getBestProvider(getCriteria(), true);
    Location location = mLocationManager.getLastKnownLocation(provider);
    if (location != null) listener.getLastKnownLocation(location);
    if (myLocationListener == null) myLocationListener = new MyLocationListener();
    mLocationManager.requestLocationUpdates(provider, minTime, minDistance, myLocationListener);
    return true;
}
 
開發者ID:angcyo,項目名稱:RLibrary,代碼行數:31,代碼來源:LocationUtils.java

示例2: register

import android.location.LocationManager; //導入方法依賴的package包/類
/**
 * 注冊
 * <p>使用完記得調用{@link #unregister()}</p>
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>}</p>
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>}</p>
 * <p>如果{@code minDistance}為0,則通過{@code minTime}來定時更新;</p>
 * <p>{@code minDistance}不為0,則以{@code minDistance}為準;</p>
 * <p>兩者都為0,則隨時刷新。</p>
 *
 * @param minTime     位置信息更新周期(單位:毫秒)
 * @param minDistance 位置變化最小距離:當位置距離變化超過此值時,將更新位置信息(單位:米)
 * @param listener    位置刷新的回調接口
 * @return {@code true}: 初始化成功<br>{@code false}: 初始化失敗
 */
public static boolean register(long minTime, long minDistance, OnLocationChangeListener listener) {
    if (listener == null) return false;
    mLocationManager = (LocationManager) Utils.getContext().getSystemService(LOCATION_SERVICE);
    mListener = listener;
    if (!isLocationEnabled()) {
        Log.d(TAG, "無法定位,請打開定位服務");
        return false;
    }
    String provider = mLocationManager.getBestProvider(getCriteria(), true);
    Location location = mLocationManager.getLastKnownLocation(provider);
    if (location != null) listener.getLastKnownLocation(location);
    if (myLocationListener == null) myLocationListener = new MyLocationListener();
    mLocationManager.requestLocationUpdates(provider, minTime, minDistance, myLocationListener);
    return true;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:31,代碼來源:LocationUtils.java

示例3: register

import android.location.LocationManager; //導入方法依賴的package包/類
/**
 * 注冊
 * <p>使用完記得調用{@link #unregister()}</p>
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>}</p>
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>}</p>
 * <p>如果{@code minDistance}為0,則通過{@code minTime}來定時更新;</p>
 * <p>{@code minDistance}不為0,則以{@code minDistance}為準;</p>
 * <p>兩者都為0,則隨時刷新。</p>
 *
 * @param minTime     位置信息更新周期(單位:毫秒)
 * @param minDistance 位置變化最小距離:當位置距離變化超過此值時,將更新位置信息(單位:米)
 * @param listener    位置刷新的回調接口
 * @return {@code true}: 初始化成功<br>{@code false}: 初始化失敗
 */
public static boolean register(long minTime, long minDistance, OnLocationChangeListener listener) {
    if (listener == null) return false;
    mLocationManager = (LocationManager) Utils.getContext().getSystemService(Context.LOCATION_SERVICE);
    mListener = listener;
    if (!isLocationEnabled()) {
        ToastUtils.showShortSafe("無法定位,請打開定位服務");
        return false;
    }
    String provider = mLocationManager.getBestProvider(getCriteria(), true);
    Location location = mLocationManager.getLastKnownLocation(provider);
    if (location != null) listener.getLastKnownLocation(location);
    if (myLocationListener == null) myLocationListener = new MyLocationListener();
    mLocationManager.requestLocationUpdates(provider, minTime, minDistance, myLocationListener);
    return true;
}
 
開發者ID:hoangkien0705,項目名稱:Android-UtilCode,代碼行數:31,代碼來源:LocationUtils.java

示例4: getLocation

import android.location.LocationManager; //導入方法依賴的package包/類
public static String getLocation(Context context) {
    if (context == null) {
        return "";
    }
    try {
        LocationManager locationManager = (LocationManager) context.getSystemService(HOME_RECOMMEND_PARAMETERS.LOCATION);
        Criteria criteria = new Criteria();
        criteria.setCostAllowed(false);
        criteria.setAccuracy(2);
        String bestProvider = locationManager.getBestProvider(criteria, true);
        if (bestProvider != null) {
            Location lastKnownLocation = locationManager.getLastKnownLocation(bestProvider);
            if (lastKnownLocation == null) {
                return "";
            }
            double latitude = lastKnownLocation.getLatitude();
            g = latitude + "*" + lastKnownLocation.getLongitude();
            return g;
        }
    } catch (Throwable e) {
        f.b("getLocation", "getLocation>>>", e);
    }
    return "";
}
 
開發者ID:JackChan1999,項目名稱:letv,代碼行數:25,代碼來源:Util.java

示例5: register

import android.location.LocationManager; //導入方法依賴的package包/類
/**
 * 注冊
 * <p>使用完記得調用{@link #unregister()}</p>
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>}</p>
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>}</p>
 * <p>如果{@code minDistance}為0,則通過{@code minTime}來定時更新;</p>
 * <p>{@code minDistance}不為0,則以{@code minDistance}為準;</p>
 * <p>兩者都為0,則隨時刷新。</p>
 *
 * @param minTime     位置信息更新周期(單位:毫秒)
 * @param minDistance 位置變化最小距離:當位置距離變化超過此值時,將更新位置信息(單位:米)
 * @param listener    位置刷新的回調接口
 * @return {@code true}: 初始化成功<br>{@code false}: 初始化失敗
 */
public static boolean register(Context context,long minTime, long minDistance, OnLocationChangeListener listener) {
    if (listener == null) return false;
    mLocationManager = (LocationManager) Utils.getContext().getSystemService(Context.LOCATION_SERVICE);
    mListener = listener;
    if (!isLocationEnabled()) {
        return false;
    }
    String provider = mLocationManager.getBestProvider(getCriteria(), true);
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return false;
    }
    Location location = mLocationManager.getLastKnownLocation(provider);
    if (location != null) listener.getLastKnownLocation(location);
    if (myLocationListener == null) myLocationListener = new MyLocationListener();
    mLocationManager.requestLocationUpdates(provider, minTime, minDistance, myLocationListener);
    return true;
}
 
開發者ID:penghongru,項目名稱:Coder,代碼行數:33,代碼來源:LocationUtils.java

示例6: getLocation

import android.location.LocationManager; //導入方法依賴的package包/類
public static String getLocation(Context context) {
    if (context == null) {
        return "";
    }
    try {
        LocationManager locationManager = (LocationManager) context.getSystemService
                ("location");
        Criteria criteria = new Criteria();
        criteria.setCostAllowed(false);
        criteria.setAccuracy(2);
        String bestProvider = locationManager.getBestProvider(criteria, true);
        if (bestProvider != null) {
            Location lastKnownLocation = locationManager.getLastKnownLocation(bestProvider);
            if (lastKnownLocation == null) {
                return "";
            }
            double latitude = lastKnownLocation.getLatitude();
            g = latitude + "*" + lastKnownLocation.getLongitude();
            return g;
        }
    } catch (Throwable e) {
        f.b("getLocation", "getLocation>>>", e);
    }
    return "";
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:26,代碼來源:Util.java

示例7: register

import android.location.LocationManager; //導入方法依賴的package包/類
/**
 * 注冊
 * <p>使用完記得調用{@link #unregister()}</p>
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>}</p>
 * <p>需添加權限 {@code <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>}</p>
 * <p>如果{@code minDistance}為0,則通過{@code minTime}來定時更新;</p>
 * <p>{@code minDistance}不為0,則以{@code minDistance}為準;</p>
 * <p>兩者都為0,則隨時刷新。</p>
 *
 * @param minTime     位置信息更新周期(單位:毫秒)
 * @param minDistance 位置變化最小距離:當位置距離變化超過此值時,將更新位置信息(單位:米)
 * @param listener    位置刷新的回調接口
 * @return {@code true}: 初始化成功<br>{@code false}: 初始化失敗
 */
public static boolean register(long minTime, long minDistance, OnLocationChangeListener listener) {
    if (listener == null) return false;
    mLocationManager = (LocationManager) Utils.getContext().getSystemService(LOCATION_SERVICE);
    mListener = listener;
    if (!isLocationEnabled()) {
        Log.d(TAG, "無法定位,請打開定位服務");
        return false;
    }
    String provider = mLocationManager.getBestProvider(getCriteria(), true);
    Location location = mLocationManager.getLastKnownLocation(provider);
    if (location != null) listener.getLastKnownLocation(location);
    if (myLocationListener == null) myLocationListener = new MyLocationListener();
   
    mLocationManager.requestLocationUpdates(provider, minTime, minDistance, myLocationListener);
    return true;
}
 
開發者ID:Wilshion,項目名稱:HeadlineNews,代碼行數:32,代碼來源:LocationUtils.java

示例8: onCreate

import android.location.LocationManager; //導入方法依賴的package包/類
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    localM = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    myProvider = localM.getBestProvider(new Criteria(), false);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
    myLocation = localM.getLastKnownLocation(myProvider);

    if (myLocation != null) {

        Log.i("WJH", myLocation.toString());

    } else {

        Log.i("WJH", "Location is null.");

    }
}
 
開發者ID:alaskalinuxuser,項目名稱:apps_small,代碼行數:30,代碼來源:MapsActivity.java

示例9: doInBackground

import android.location.LocationManager; //導入方法依賴的package包/類
@Override
protected Integer doInBackground(Context... params) {

    HttpResponse response;
    try {
        LocationManager lm = (LocationManager) params[0].getSystemService(Context.LOCATION_SERVICE);
        Criteria crit = new Criteria();
        crit.setAccuracy(Criteria.ACCURACY_FINE);
        String provider = lm.getBestProvider(crit, true);

        Location loc = lm.getLastKnownLocation(provider);

        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();

        request.setURI(new URI(Utils.WEATHER_URL + "lat=" +
                loc.getLatitude() +
                "&lon=" +
                +loc.getLongitude()
                + "&units=metric"));

        response = client.execute(request);
        String result = EntityUtils.toString(response.getEntity());
        JSONObject jsonResponse = new JSONObject(result);
        JSONObject jsonWeather = jsonResponse.getJSONObject("main");
        return jsonWeather.getInt("temp");

    } catch (Exception e) {
        error = e;
    }
    return 0;
}
 
開發者ID:feup-infolab,項目名稱:labtablet,代碼行數:33,代碼來源:AsyncWeatherFetcher.java

示例10: getLastKnownLocation

import android.location.LocationManager; //導入方法依賴的package包/類
public static Location getLastKnownLocation(Context context) {
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    final Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.POWER_HIGH);
    final String providerName = locationManager.getBestProvider(criteria, true);
    if (providerName == null)
        return null;
    if (PermissionHelper.checkCoarseLocationPermission(context)) {
        return locationManager.getLastKnownLocation(providerName);
    } else {
        return null;
    }
}
 
開發者ID:rtr-nettest,項目名稱:open-rmbt,代碼行數:16,代碼來源:GeoLocation.java

示例11: getLocation

import android.location.LocationManager; //導入方法依賴的package包/類
public Location getLocation(Activity activity)
{
    LocationManager locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
    // 查找到服務信息
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗

    String provider = locationManager.getBestProvider(criteria, false); // 獲取GPS信息
    Location location = locationManager.getLastKnownLocation(provider); // 通過GPS獲取位置
    return location;
}
 
開發者ID:SShineTeam,項目名稱:Huochexing12306,代碼行數:16,代碼來源:ShareUtil.java

示例12: isLocationEnable

import android.location.LocationManager; //導入方法依賴的package包/類
public static boolean isLocationEnable(Context context) {
	LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
	Criteria cri = new Criteria();
	cri.setAccuracy(Criteria.ACCURACY_COARSE);
	cri.setAltitudeRequired(false);
	cri.setBearingRequired(false);
	cri.setCostAllowed(false);
	String bestProvider = locationManager.getBestProvider(cri, true);
	return !TextUtils.isEmpty(bestProvider);
	
}
 
開發者ID:newDeepLearing,項目名稱:decoy,代碼行數:12,代碼來源:NimLocationManager.java

示例13: selectCurrentLocation

import android.location.LocationManager; //導入方法依賴的package包/類
protected void selectCurrentLocation(boolean forceUseGps) {
       setCurrentLocation = true;
       selectedLocationId = 0;

	if (transaction.isTemplateLike()) {
		if (isShowLocation) {
			locationText.setText(R.string.current_location);
		}
		return;
	}		
	      
       // Start listener to find current location
       locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
       String provider = locationManager.getBestProvider(new Criteria(), true);
       
       if (provider != null) {
       	lastFix = locationManager.getLastKnownLocation(provider);        	
       }  

       if (lastFix != null) {
       	setLocation(lastFix);
       	connectGps(forceUseGps);
       } else {
       	// No enabled providers found, so disable option
       	if (isShowLocation) {
       		locationText.setText(R.string.no_fix);
       	}
       }
}
 
開發者ID:tiberiusteng,項目名稱:financisto1-holo,代碼行數:30,代碼來源:AbstractTransactionActivity.java

示例14: onCreate

import android.location.LocationManager; //導入方法依賴的package包/類
protected void onCreate(Bundle paramBundle)
{
    super.onCreate(paramBundle);
    setContentView(R.layout.activity_maps);

    localdb = openOrCreateDatabase("localdb",MODE_PRIVATE,null);
    localdb.execSQL("CREATE TABLE IF NOT EXISTS position(imei NVARCHAR(100), latitude NVARCHAR(100),longitude NVARCHAR(100), timestamp DATETIME, out NVARCHAR(1));");
    localdb.execSQL("DELETE FROM position WHERE timestamp <= date('now','-2 day');");   // to keep data in last day on user phone

    setUpMapIfNeeded();
    this.mMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    this.mMap.setMyLocationEnabled(true);

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(provider);

    locationManager.requestLocationUpdates(provider, 20000, 0, this);

    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String myImei = tm.getDeviceId();

    timer = new Timer();
    timer.schedule(new RunMarker(this.mMap, myImei,this), 0, 20000);

    editTextFromDate = (EditText) findViewById(R.id.editText);
    search = (Button) findViewById(R.id.button);
    DateField fromDate = new DateField(editTextFromDate,search, this, mMap, timer);
}
 
開發者ID:sarikaya,項目名稱:Bee-Analyzer,代碼行數:31,代碼來源:MapsActivity.java

示例15: onCreate

import android.location.LocationManager; //導入方法依賴的package包/類
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    localM = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    myProvider = localM.getBestProvider(new Criteria(), false);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
    myLocation = localM.getLastKnownLocation(myProvider);

    if (myLocation != null) {

        Log.i("WJH", myLocation.toString());

    } else {

        Log.i("WJH", "Location is null.");

    }

}
 
開發者ID:alaskalinuxuser,項目名稱:apps_small,代碼行數:27,代碼來源:MainActivity.java


注:本文中的android.location.LocationManager.getBestProvider方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。