博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android gps定位
阅读量:6927 次
发布时间:2019-06-27

本文共 4850 字,大约阅读时间需要 16 分钟。

hot3.png

package com.pdager.gps;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.location.GpsSatellite;import android.location.GpsStatus;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.provider.Settings;import android.text.format.Time;import android.view.KeyEvent;import android.widget.TextView;import android.widget.Toast;public class getlocation extends Activity {	/** Called when the activity is first created. */	@Override	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		openGPSSettings();	}	private void 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();			getLocation();			return;		}		else{		Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();		Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);		startActivityForResult(intent, R.layout.main); // 此为设置完成后返回到获取界面		getLocation();		return;		}	}	LocationManager locationManager;	private void getLocation() {		// 获取位置管理服务		String serviceName = Context.LOCATION_SERVICE;		locationManager = (LocationManager) this.getSystemService(serviceName);		// 查找到服务信息		// Criteria criteria = new Criteria();		// criteria.setAccuracy(Criteria.ACCURACY_FINE);		// // 高精度		// criteria.setAltitudeRequired(false);		// criteria.setBearingRequired(false);		// criteria.setCostAllowed(true);		// criteria.setPowerRequirement(Criteria.POWER_LOW);		// // 低功耗		// String provider = locationManager.getBestProvider(criteria, true);		// 获取GPS信息		String provider = LocationManager.GPS_PROVIDER;		Location location = locationManager.getLastKnownLocation(provider);// 通过GPS获取位置		if (location == null)			location = locationManager					.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);		updateToNewLocation(location);		// 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000)或最小位移变化超过N米		locationManager.requestLocationUpdates(provider, 1000, 0,				locationListener);		locationManager.addGpsStatusListener(statusListener); // 注册状态信息回调	}	private List
 numSatelliteList = new ArrayList
(); // 卫星信号 /**  * 卫星状态监听器  */ private final GpsStatus.Listener statusListener = new GpsStatus.Listener() { public void onGpsStatusChanged(int event) { // GPS状态变化时的回调,如卫星数 GpsStatus status = locationManager.getGpsStatus(null); // 取当前状态 updateGpsStatus(event, status); } }; private void updateGpsStatus(int event, GpsStatus status) { if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) { int maxSatellites = status.getMaxSatellites(); Iterator
 it = status.getSatellites().iterator(); numSatelliteList.clear(); int count = 0; while (it.hasNext() && count <= maxSatellites) { GpsSatellite s = it.next(); numSatelliteList.add(s); count++; } } } private void updateToNewLocation(Location location) { // 获取系统时间 Time t = new Time(); t.setToNow(); // 取得系统时间 int year = t.year; int month = t.month + 1; int date = t.monthDay; int hour = t.hour; // 24小时制 int minute = t.minute; int second = t.second; TextView tv1; tv1 = (TextView) this.findViewById(R.id.tv1); if (location != null) { double latitude = location.getLatitude();// 经度 double longitude = location.getLongitude();// 纬度 double altitude = location.getAltitude(); // 海拔 tv1.setText("搜索卫星个数:" + numSatelliteList.size() + "/n纬度:" + latitude + "/n经度:" + longitude + "/n海拔:" + altitude + "/n时间:" + year + "年" + month + "月" + date + "日" + hour + ":" + minute + ":" + second); } else { tv1.setText("无法获取地理信息"); } } private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { // 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发 if (location != null) { updateToNewLocation(location); Toast.makeText(getlocation.this, "您的位置已发生改变!", Toast.LENGTH_SHORT).show(); } } public void onProviderDisabled(String provider) { // Provider被disable时触发此函数,比如GPS被关闭 updateToNewLocation(null); } public void onProviderEnabled(String provider) { // Provider被enable时触发此函数,比如GPS被打开 } public void onStatusChanged(String provider, int status, Bundle extras) { // Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数 } }; public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_MENU) {// 拦截menu键事件 // do something... System.exit(0); } if (keyCode == KeyEvent.KEYCODE_BACK) {// 拦截返回按钮事件 // do something... System.exit(0); } return true; }}

转载于:https://my.oschina.net/zhulunjun/blog/206400

你可能感兴趣的文章
2017-5-10 小型人员管理系统
查看>>
单例模式
查看>>
G - Bullseye
查看>>
网络对抗技术作业(PPSUC)
查看>>
wav文件格式分析
查看>>
管理node的版本
查看>>
PIC 里面关于 __CONFIG( ) 配置位
查看>>
docker 使用swarm overlay网络时,报“network xx not manually attachable”错误解决
查看>>
tomcat各目录下的作用
查看>>
AtCoder Regular Contest 082
查看>>
服务端 | Nodejs 学习笔记(一)
查看>>
毕业设计第二次任务书
查看>>
Instrument详解
查看>>
Date3
查看>>
【以前的空间】系列
查看>>
python文件处理
查看>>
再论三层架构
查看>>
Samba再报安全漏洞
查看>>
数字签名工作原理
查看>>
浅析流程管理活动的实施要领
查看>>