溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android獲取用戶location值為NULL的解決辦法的代碼

發(fā)布時(shí)間:2020-06-18 12:19:15 來(lái)源:網(wǎng)絡(luò) 閱讀:298 作者:wwawajiao 欄目:移動(dòng)開發(fā)

下面的代碼內(nèi)容是關(guān)于Android獲取用戶location值為NULL的解決辦法的代碼,應(yīng)該是對(duì)碼農(nóng)有一些用處。

package com.jeedroid.tools;   

import java.io.IOException;   
import java.util.List;   
import java.util.Locale;   

import android.content.Context;   
import android.location.Address;   
import android.location.Criteria;   
import android.location.Geocoder;   
import android.location.Location;   
import android.location.LocationListener;   
import android.location.LocationManager;   
import android.os.Bundle;   
import android.provider.Settings;   
import android.util.Log;   

public class LocationTools   
{   
    private Location location;   
public static LocationManager getLocationManager(Context context)   
{   
    return (LocationManager) context.getSystemService(context.LOCATION_SERVICE);   
}   
public String getAddress(Context context)   
{   
    LocationManager locationManager = this.getLocationManager(context);   
    if(!locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))   
    {   
    android.provider.Settings.Secure.setLocationProviderEnabled(context.getContentResolver(), LocationManager.GPS_PROVIDER, false);   
    }   
    return doWork(context);   
}   

private String doWork(Context context)   
{   
    String addres="";   
    LocationManager locationManager = this.getLocationManager(context);   
    Criteria criteria = new Criteria();   
    criteria.setAccuracy(Criteria.ACCURACY_FINE);     
    criteria.setAltitudeRequired(false);     
    criteria.setBearingRequired(false);     
    criteria.setCostAllowed(false);     
    criteria.setPowerRequirement(Criteria.POWER_LOW);     
    String provider =locationManager.getBestProvider(criteria, true);   
    Log.i("provider>>>>>>", provider);   
    while(location==null)   
    {   
    location =locationManager.getLastKnownLocation(provider);   
    }   
    LocationListener locationListener = new LocationListener()   
    {   

        @Override  
        public void onLocationChanged(Location location)   
        {   
            LocationTools.this.location=location;   
        }   

        @Override  
        public void onProviderDisabled(String provider)   
        {   

        }   

        @Override  
        public void onProviderEnabled(String provider)   
        {   

        }   

        @Override  
        public void onStatusChanged(String provider, int status, Bundle extras)   
        {   

        }   

    };   
    locationManager.requestLocationUpdates(provider, 1000, 10, locationListener);   

    Geocoder geo = new Geocoder(context,Locale.getDefault());   
    try  
    {   
        List<Address> address=geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);   
        if(address.size()>0)   
        {   
            addres=address.get(0).getAddressLine(0);   
        }   
    } catch (IOException e)   
    {   
        e.printStackTrace();   
    }   
    return addres;   
}   
}  
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI