Android Location在GPS中的应用(二)

package com.example.location;

import java.util.List;

import java.util.Locale;

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.os.IBinder;
import android.provider.Settings;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private LocationManager locationManager;

	private String provider;
	private Location location;
	private Address address;
	private TextView textview;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		locationManager = (LocationManager) this
				.getSystemService(Context.LOCATION_SERVICE);
		getProvider();
		openGPS();
		// 获取位置
		location = locationManager.getLastKnownLocation(provider);
		// 显示位置信息
		updateWithNewLocation(location);
		// 注册监听器locationListener,
		locationManager.requestLocationUpdates(provider, 2000, 10,
				locationListener);

	}

	private void openGPS() {
		if (locationManager
				.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)
				|| locationManager
						.isProviderEnabled(android.location.LocationManager.NETWORK_PROVIDER)) {
			Toast.makeText(this, "位置源已设置", Toast.LENGTH_SHORT).show();
			return;
		}
		Toast.makeText(this, "位置源未设置!", Toast.LENGTH_SHORT).show();
		Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
		startActivityForResult(intent, 0);
	}

	private void getProvider() {
		// 构建查询条件
		Criteria criteria = new Criteria();
		// 查询精度:高
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		// 是否查询海拔:是
		criteria.setAltitudeRequired(true);
		// 是否查询方位角:是
		criteria.setBearingRequired(true);
		// 是否付费:是
		criteria.setCostAllowed(true);
		// 电量要求:低
		criteria.setPowerRequirement(Criteria.POWER_LOW);

		// 返回最合适的符合条件的provider,第二个参数为true说明如果只有一个provider是有效地,则返回当前provider;
		provider = locationManager.getBestProvider(criteria, true);

	}

	// GPS消息监听器
	private final LocationListener locationListener = new LocationListener() {
		// 位置发生改变后调用
		public void onLocationChanged(Location location) {
			updateWithNewLocation(location);
		}

		// provider被用户关闭后调用
		public void onProviderDisabled(String provider) {
			updateWithNewLocation(null);
		}

		// provider被用户开启后调用
		public void onProviderEnabled(String provider) {
		}

		// provider状态变化时调用
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	};

	// GPS监听器调用,处理位置信息
	private void updateWithNewLocation(Location location) {
		String latLongString;
		TextView myLocationText = (TextView) findViewById(R.id.textview);
		String string = "GPS 信息详情:";
		myLocationText.setTextColor(Color.RED);
		myLocationText.setTextSize(20);
		if (location != null) {
			double lat = location.getLatitude();
			double lng = location.getLongitude();
			latLongString = "纬度" + lat + "/n经度" + lng;
		} else {
			latLongString = "无法获取地理信息";
		}
		myLocationText.setText(string + "/n" + "您当前的位置:/n" + latLongString
				+ "/n" + getAddressbyGeoPoint(location));
	}

	// 获取地址信息
	private List<Address> getAddressbyGeoPoint(Location location) {
		List<Address> result = null;
		try {
			if (location != null) {
				// 获取Geocoder,通过Geocoder就可以拿到地址
				Geocoder gc = new Geocoder(this, Locale.getDefault());
				result = gc.getFromLocation(location.getLatitude(),
						location.getLongitude(), 1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		super.onCreateOptionsMenu(menu);
		menu.add(0, Menu.FIRST + 1, 1, "打开GPS");
		menu.add(0, Menu.FIRST + 2, 2, "关闭GPS");
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		Intent i;
		super.onOptionsItemSelected(item);
		switch(item.getItemId()) {
		case Menu.FIRST + 1;
		this.setTitle("GPS Started");
		i = new Intent(this,GpsService.class);
		this.bindService(i, connection, Context.BIND_AUTO_CREATE);
		break;
		case Menu.FIRST+2;
		this.setTitle("GPS Stopped");
		if ( mBinder != null ){ 
			i = new Intent( this , GpsService. class ); 
			this .unbindService( connection ); 
			mBinder=null;//释放mBinder,防止重复解绑定
		}
		break;
	    }
		return true;
    }

	ServiceConnection connection = new ServiceConnection() {
		public void onServiceConnected(ComponentName arg0, IBinder agr1) {
			mBinder = (IGpsBinder) arg1;
			if (mBinder != null) {
				mBinderbindService(main.this);
			}
		}

		public void onServiceDisconnected(ComponentName name) {
		}

	};

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值