写了好久,一直没有结果,主要是这个方法
第二个参数是时间2000为2秒。第三个参数是10,其实就是距离。
真机调试要走动一下,就有结果了。
LocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, this);
WeFragment.java
package com.fragmentwechattest.frag;
import java.io.IOException;
import java.util.List;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.fragmentwechattest.R;
import com.fragmentwechattest.utils.ShareUtils;
public class WeFragment extends Fragment implements LocationListener {
private String tag="WeFragment";
private LocationManager LocationManager;//定位的管理器类
private String cityName;//当前城市的名称
private TextView textViewCity;
private TextView textViewFirstPage;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i("WeFragment", "onCreateView");
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.we_fragment, container, false);
// View view=inflater.inflate(R.layout.we_fragment,null);
textViewCity = (TextView) view.findViewById(R.id.my_wechat_fragment_city);
textViewFirstPage = (TextView) view.findViewById(R.id.my_wechat_fragment_scanner);
//获取数据并且显示
textViewCity.setText(ShareUtils.getWelcomeString(getActivity()));
return view;
}
@Override
public void onStart() {
// TODO Auto-generated method stub
//检查gps
Log.i("WeFragment", "onStart");
super.onStart();
checkGpsIsOpen();
}
private void checkGpsIsOpen() {
// TODO Auto-generated method stub
//获取当前的locationManger的对象
Log.i("WeFragment", "checkGpsIsOpen");
LocationManager=(LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
boolean isopen=LocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(!isopen){
//进入gps的设置页面
Intent intent = new Intent();
intent.setAction(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, 0);
}
//开始定位
startLocation();
}
private void startLocation() {
// 定位TODO Auto-generated method stub
//开启定位
Log.i("WeFragment", "startLocation");
LocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, this);
}
// 接收并且处理消息
private Handler handler=new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
Log.i("WeFragment", "handler");
// TODO Auto-generated method stub
if(msg.what==1){
textViewCity.setText(cityName);
}
return false;
}
});
//获取对应位置的经纬度,获取位置。
private void updateWithNewLocation(Location location) {
// TODO Auto-generated method stub
Log.i("WeFragment", "updateWithNewLocation");
double lat=0.0,lng=0.0;
if(location!=null){
lat=location.getLatitude();
lng=location.getLongitude();
Log.i(tag, lat+"1="+lng);
}else{
Log.i(tag, lat+"2="+lng);
cityName="无法获取城市信息";
}
//通过经纬度获取地址,
List<Address> list=null;
//经纬度返回地址
Geocoder ge=new Geocoder(getActivity());
try {
list=ge.getFromLocation(lat, lng, 2);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(list!=null&&list.size()>0){
for (int i = 0; i < list.size(); i++) {
Address ad=list.get(i);
cityName=ad.getLocality();//获取城市信息
Log.i(tag, cityName);
}
Log.i(tag,cityName);
}
handler.sendEmptyMessage(1);
}
//位置信息更改执行的方法
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
updateWithNewLocation(location);
Log.i("WeFragment", "onLocationChanged");
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Log.i("WeFragment", "onProviderEnabled");
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Log.i("WeFragment", "onProviderDisabled");
}
//定位状态发生改变
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Log.i("WeFragment", "onStatusChanged");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i("WeFragment", "onDestroy");
super.onDestroy();
//保存定位信息
ShareUtils.putWelcomeString(getActivity(), cityName);
//保存当前的城市
stopLocation();
}
//停止定位
private void stopLocation(){
Log.i("WeFragment", "stopLocation");
LocationManager.removeUpdates(this);
}
}