Android—定位
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bt_main_checkin;
private Toolbar toolbar;
private TextView tv_main_test;
private LocationManager mLocationManager;
private static final int TEN_SECONDS = 10000;
private static final int TEN_METERS = 10;
private static final int TWO_MINUTES = 1000 * 60 * 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
bt_main_checkin.setOnClickListener(this);
setSupportActionBar(toolbar);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location LatLng = setup();
// Toast.makeText(this, LatLng.getLatitude() + "|" + LatLng.getLongitude(), Toast.LENGTH_SHORT).show();
tv_main_test.setText(LatLng.getLatitude() + "|" + LatLng.getLongitude());
}
private void initView() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
bt_main_checkin = (Button) findViewById(R.id.bt_main_checkin);
tv_main_test= (TextView) findViewById(R.id.tv_main_test);
}
@Override
protected void onResume() {
super.onResume();
setup();
}
@Override
protected void onStop() {
super.onStop();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocationManager.removeUpdates(listener);
}
@Override
protected void onStart() {
super.onStart();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
final boolean gpsenable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsenable) {
new EnableGpsDialogFragment().show(getFragmentManager(), "enableGpsDialog");
}
}
@Override
public void onClick(View v) {
}
private Location setup() {
Location gpsLocation = null;
Location networkLocation = null;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
new EnableGpsDialogFragment().show(getFragmentManager(), "enableGpsDialog");
new EnableNetworkDialogFragment().show(getFragmentManager(), "enableNetworkDialog");
return null;
}
mLocationManager.removeUpdates(listener);
gpsLocation = requestUpdatesFromProvider(LocationManager.GPS_PROVIDER, R.string.not_support_gps);
networkLocation = requestUpdatesFromProvider(LocationManager.NETWORK_PROVIDER, R.string.not_support_network);
if (gpsLocation != null && networkLocation != null) {
return getBetterLocation(gpsLocation, networkLocation);
} else if (gpsLocation != null) {
return gpsLocation;
} else if (networkLocation != null) {
return networkLocation;
}
return null;
}
private Location requestUpdatesFromProvider(final String provider, final int errorResId) {
Location location = null;
if (mLocationManager.isProviderEnabled(provider)) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
return null;
}
mLocationManager.requestLocationUpdates(provider, TEN_SECONDS, TEN_METERS, listener);
location = mLocationManager.getLastKnownLocation(provider);
} else {
Toast.makeText(this, errorResId, Toast.LENGTH_SHORT).show();
}
return location;
}
private final LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// TODO: 2016/3/23 updata ui in it
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
protected Location getBetterLocation(Location newLocation, Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return newLocation;
}
// If it's been more than two minutes since the current location, use the new location
// because the user has likely moved.
long timeDelta = newLocation.getTime() - currentBestLocation.getTime();
boolean isSignficantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignficantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
if (isSignficantlyNewer) {
return newLocation;
} else if (isSignficantlyOlder) {
return currentBestLocation;
}
// Determine location quality using a combination of timeliness and accuracy
int accuracyDelta = (int) (newLocation.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
boolean isFromSameProvider = isSameProvider(newLocation.getProvider(),
currentBestLocation.getProvider());
if (isMoreAccurate) {
return newLocation;
} else if (isNewer && !isLessAccurate) {
return newLocation;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
return newLocation;
}
return currentBestLocation;
}
/**
* Checks whether two providers are the same
*/
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id){
case R.id.action_settings:
break;
case R.id.action_register:
break;
default:
break;
}
//noinspection SimplifiableIfStatement
// if (id == R.id.action_settings) {
// return true;
// }
return true;
}
}
详细见官网
Android—定位的更多相关文章
- android 定位的四种方式
[原文] 开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面总结了一下网络中现有对于介绍android定位的4种方式,希望对大家有帮助: android 定 ...
- Android定位功能
不说废话,直接说说实现android定位有关的API吧. 这些API都在android.location包下,一共有三个接口和八个类.它们配合使用即可实现定位功能. 三个接口: GpsStatus.L ...
- Android定位功能(二)
在前文Android定位功能(一)中,已经大致介绍了一下在Android平台中,和定位功能相关的类,并举例获取了位置信息.但是前文是基于Criteria定制了一个标准,通过getBestProvide ...
- Android定位测试(深坑)
问题:我们是一个海外app,市场部去马来西亚打开那边的市场,发现了一个问题,就是我们的app定位有问题,还是成都的定位,主要原因是在马来西亚使用这个app,请求中带的经纬度参数是成都的,导致服务器返回 ...
- android 定位的几种方式介绍
[地理位置] android 定位的几种方式介绍 开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面 www.androidkaifa.com 总结了一下网络 ...
- 六 APPIUM Android 定位方式
文本转自:http://www.cnblogs.com/sundalian/p/5629500.html APPIUM Android 定位方式 1.定位元素应用元素 1.1通过id定位元素 An ...
- 发现最新版百度Android 定位SDK v6.1.3 网络定位bug
对于百度地图已经实在忍无可忍了,实验室两年以前的一个项目用到了百度地图,以前师兄毕业了,我来维护这个破项目,百度地图推出新版本出来后,老版本的api不能用了,不能做到向下兼容吗?换掉少量的api也就算 ...
- Appium学习路—Android定位元素与操作
一.常用识别元素的工具 uiautomator:Android SDK自带的一个工具,在tools目录下 monitor:Android SDK自带的一个工具,在tools目录下 Appium Ins ...
- 目前主流的Android定位有如下几种:
1.通过GPS模块 GPS方式准确度是最高的,但是它的缺点也非常明显:1,比较耗电:2,绝大部分用户默认不开启GPS模块:3,从GPS模块启动到获取第一次定位数据,可能需要比较长的时间:4,室内几乎无 ...
- android定位
先说说手机定位的方式 1,GPS 绝大部分手机都有GPS模块,这种方式准确度是最高的,但是缺点也很明显,1,耗电高:2,绝大部分用户默认不开启GPS模块.3,从GPS模块启动到获取第一次定位数据,可能 ...
随机推荐
- iOS开发之山寨版新浪微博小结
在之前的博客IOS开发之新浪围脖中获取微博的内容是使用我自己的access_token来请求的数据,那么如何让其他用户也能登陆并获取自己的微博内容呢?接下来就是OAuth和SSO出场的时候啦.OAut ...
- 玩转Jquery,告别前端知道思路忘记知识点的痛苦
本节内容: 本章主要讲解一下jquery,主要是工作中用的前端框架是datetables框架,然后datetables框架又是基于jqeury研发的,所以要想学一个东西,就必须要了解其底层,不然走路都 ...
- Android APK如何签名
Android项目以它的包名作为唯一标识,如果在同一设备上安装两个相同的应用,后面安装的应用就会覆盖前面安装的应用.为了避免这种情况的发生,我们需要对作为产品发布的应用进行签名. 签名其实有两个作用: ...
- Front End Developer Questions 前端开发人员问题(三)JavaScript部分
问题来源:http://markyun.github.io/2015/Front-end-Developer-Questions/ 三.javascript1.介绍JavaScript的基本数据类型. ...
- 四大组件之ContentProvider
前言 ContentProvider作为Android的四大组件之一,是属于需要掌握的基础知识,可能在我们的应用中,对于Activity和Service这两个组件用的很常见,了解的也很多,但是对Con ...
- [Web API] Web API 2 深入系列(1) 路由
目录 ASP.NET 路由 注册路由 动态映射HttpHandler WebAPI 路由 注册路由 调用GetRouteData 2个路由系统衔接 GlobalConfiguration Hosted ...
- 用javascript编写的小游戏(getElementById , setInterval , clearInterval , window.onload , innerText 和页面跳转, 标签的使用)
(1)图片轮转 <script type="text/javascript" > ; setInterval(function(){ var dom=document. ...
- [WCF编程]12.事务:服务事务编程(下)
一.投票与提交 虽然WCF负责事务传播及两阶段提交协议的管理工作,但是 她不知道事务是否应该提交或终止.这需要根服务告诉WCF应该何时启动两阶段提交协议.是提交还是终止.WCF提供了两种编程模式来对事 ...
- 解决WebApi入参时多对象的问题
我们的项目是用WebApi提供数据服务,且WebPage跟APP中都有调用到. WebApi提供的接口一多,就发现一个问题,我们项目中有很多接口是接收POST(安全原因,我们采用的是https)请求的 ...
- Entity Framework 代码先行之约定配置
要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就 ...