在玩一个GPS的东西 获取Location一直为null 后来是从onLocationChanged里面拿location才搞定了 贴一下吧 代码太乱
/**
* 主要是用户定位操作
*@author hope
*/
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;
publicclass LocationTools
{
private Location location;
publicstatic 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))
{
//打开GPS 需Android2.2以上系统支持
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);
//获得当前位置 location为空是一直取 从onLocationChanged里面取
while(location==null)
{
location =locationManager.getLastKnownLocation(provider);
}
//locationListener
LocationListener locationListener = new LocationListener()
{
@Override
publicvoid onLocationChanged(Location location)
{
LocationTools.this.location=location;
}
@Override
publicvoid onProviderDisabled(String provider)
{
}
@Override
publicvoid onProviderEnabled(String provider)
{
}
@Override
publicvoid 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)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return addres;
}
}
今天弄了一个多小时,写了一个GPS获取地理位置代码的小例子,包括参考了网上的一些代码,并且对代码进行了一些修改,希望对大家的帮助。具体代码如下: 要实用Adnroid平台的GPS设备,首先需要添加上权限,所以需要添加如下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
具体实现代码如下:
首先判断GPS模块是否存在或者是开启:


privatevoid openGPSSettings() {
LocationManager alm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
if (alm
.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT)
.show();
return;
}
Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent,0); //此为设置完成后返回到获取界面
}
如果开启正常,则会直接进入到显示页面,如果开启不正常,则会进行到GPS设置页面:
获取代码如下:
到这里就可以获取到地理位置信息了,但是还是要显示出来,那么就用下面的方法进行显示:


privatevoid updateToNewLocation(Location location) {
TextView tv1;
tv1 = (TextView) this.findViewById(R.id.tv1);
if (location != null) {
double latitude = location.getLatitude();
double longitude= location.getLongitude();
tv1.setText("维度:" + latitude+ "\n经度" + longitude);
} else {
tv1.setText("无法获取地理信息");
}
}
android gps打开和关闭
http://blog.youkuaiyun.com/zfzf294990051/article/details/7393966
http://blog.youkuaiyun.com/mrandexe/article/details/6621125
Android GPS 定位的实现(1)
http://www.cnblogs.com/fly_binbin/archive/2010/12/16/1908518.html
Android 强制开启GPS
http://download.youkuaiyun.com/detail/liqingang/4350556
Android判断GPS是否开启和强制帮用户打开GPS
http://blog.youkuaiyun.com/android_ls/article/details/8605931
关于4.0强制开启GPS定位
http://www.eoeandroid.com/thread-199085-1-1.html
转载于:https://blog.51cto.com/4789781/1259981