新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Android获取用户location值为NULL的解决办法的代码

下面的代码内容是关于Android获取用户location值为NULL的解决办法的代码,应该是对码农有一些用处。

创新互联公司主要从事成都网站设计、网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务鲁山,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

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=geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1); if(address.size()>0) { addres=address.get(0).getAddressLine(0); } } catch (IOException e) { e.printStackTrace(); } return addres; } }

网站名称:Android获取用户location值为NULL的解决办法的代码
分享链接:http://wjwzjz.com/article/ghiech.html
在线咨询
服务热线
服务热线:028-86922220
TOP