android的locationManager—执行onLocationChanged方法.

写了好久,一直没有结果,主要是这个方法

第二个参数是时间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);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android 中的 LocationManager 是用于获取设备当前位置信息的类,下面是使用 LocationManager 的基本方法: 1. 获取 LocationManager 实例: 要使用 LocationManager,首先需要获取它的实例。可以通过 getSystemService() 方法来获取系统级的 LocationManager 实例,例如: LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 2. 设置位置更新监听器: 要获取设备位置信息,需要注册一个位置更新监听器。可以通过 locationManager 的 requestLocationUpdates() 方法来设置监听器,设置监听器需要指定位置信息的更新频率和最小更新距离等参数: locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener); 3. 实现位置监听器: 需要自定义一个 LocationListener 的实现类来处理位置信息的更新。在 LocationListener 的回调方法中可以处理获取到的位置信息,例如: private class MyLocationListener implements LocationListener { @Override public void onLocationChanged(Location location) { // 处理获取到的位置信息 } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // 位置提供者状态变化时的处理 } @Override public void onProviderEnabled(String provider) { // 位置提供者启用时的处理 } @Override public void onProviderDisabled(String provider) { // 位置提供者禁用时的处理 } } 4. 获取最后已知的位置信息: 除了实时获取设备位置信息外,还可以通过 LocationManager 的 getLastKnownLocation() 方法来获取设备最后一次已知的位置信息,例如: Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 通过以上方法,可以使用 LocationManager 来获取设备的当前位置信息,并且可以在位置信息更新时及时进行处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值