当前位置: 首页 > news >正文

网站建设上线问题网站开发软件科技公司

网站建设上线问题,网站开发软件科技公司,电影网站这么做关键词,英文版企业网站布局设计目录 一、定位功能准备 1.申请权限 2.开启定位所需功能 #xff08;1#xff09;定位 #xff08;2#xff09;WiFi #xff08;3#xff09;移动数据连接#xff08;基站#xff09; 二、获取定位信息 1.定位条件器 Criteria 2.定位管理器 LocationManager …目录 一、定位功能准备 1.申请权限 2.开启定位所需功能 1定位 2WiFi 3移动数据连接基站 二、获取定位信息 1.定位条件器 Criteria 2.定位管理器 LocationManager 3.定位监听器 LocationListener 三、解析定位信息 Location 四、案例代码一览 Android的手机定位一般由卫星定位、WiFi定位、基站定位实现。卫星定位由几个全球卫星导航系统提供主要包括美国GPS、俄罗斯格洛纳斯、中国北斗。WiFi定位一般通过接入公共WiFi通过WiFi的MAC地址与电信网络宽带的网络IP查询WiFi位置获取接入该WiFi的大致位置。基站定位是监测手机SIM卡能搜索到周围的哪些基站手机必然处于它们信号的重叠位置。 一、定位功能准备 定位需要手机开启对应的功能定位、无线网络(WiFi)、数据连接。 1.申请权限 定位操作需要获取权限部分权限涉及用户隐私需要动态申请。 !--定位-- !--精准定位一般通过卫星定位需要动态申请权限-- uses-permission android:nameandroid.permission.ACCESS_FINE_LOCATION/ !--粗略定位一般通过基站定位或WiFi定位需要动态申请权限-- uses-permission android:nameandroid.permission.ACCESS_COARSE_LOCATION/!--查看网络状态-- !--数据网络状态无需动态申请-- uses-permission android:nameandroid.permission.ACCESS_NETWORK_STATE/ !--WiFi网络状态无需动态申请-- uses-permission android:nameandroid.permission.ACCESS_WIFI_STATE/!--查看手机状态需要动态申请-- uses-permission android:nameandroid.permission.READ_PHONE_STATE/ 2.开启定位所需功能 1定位 使用LocationManager定位管理器获取定位功能状态。 //获取定位功能状态 public static boolean getGPSState(Context context){//获取定位管理器LocationManager locationManager (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); }//确认定位功能是否开启未开启则跳转至定位功能设置界面 public static void checkGPSIsOpen(Context context){//未开启定位功能if(!getGPSState(context)){//跳转至定位功能设置界面Intent intentnew Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);context.startActivity(intent);} } 2WiFi 使用WiFiManagerWiFi管理器获取WiFi状态及设置WiFi状态。 //获取WiFi状态 public static boolean getWiFiState(Context context){WifiManager wifiManager (WifiManager) context.getSystemService(Context.WIFI_SERVICE);return wifiManager.isWifiEnabled(); }//设置WiFi状态 public static void setWiFiState(Context context,boolean state){WifiManager wifiManager (WifiManager) context.getSystemService(Context.WIFI_SERVICE);wifiManager.setWifiEnabled(state); } 3移动数据连接基站 使用ConnectivityManager连接管理器获取移动数据连接状态及设置连接状态请注意因为是隐藏方法需要通过反射调用。 //获取移动数据连接开关的状态 public static boolean getMobileDataState(Context context){//获取连接管理器ConnectivityManager connectivityManager (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);boolean isOpenfalse;try {//该方法为隐藏方法需要通过反射调用String methodNamegetMobileDataEnable;Method methodconnectivityManager.getClass().getMethod(methodName);isOpen (boolean) method.invoke(connectivityManager);}catch (Exception e){e.printStackTrace();}return isOpen; }//设置移动数据连接开关的状态 public static void setMobileDataState(Context context,boolean state){//获取连接管理器ConnectivityManager connectivityManager (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);try{//该方法为隐藏方法需要通过反射调用String methodNamesetMobileDataEnable;Method methodconnectivityManager.getClass().getMethod(methodName);method.invoke(connectivityManager,state);}catch (Exception e){e.printStackTrace();} } 二、获取定位信息 开启定位相关功能只是将定位的前提条件准备好若想获得手机当前所处的位置信息还要依靠一系列定位工具。与定位信息获取有关的工具有定位条件器Criteria、定位管理器LocationManager、定位监听器LocationListener。 简单来说就是通过设置好条件的定位条件器(Criteria)获取到最佳的定位信息提供者(String型数据)再向定位管理器添加定位监听器(LocationListener)及定位信息提供者。 下面对这3个工具分别进行介绍。 1.定位条件器 Criteria 定位条件器用于设置定位的前提条件比如精度、速度、海拔、方位等信息。 有以下6个常用方法 setAccuracy设置定位精确度。有两个取值 Criteria.ACCURACY_FINE 表示精度高。Criteria.ACCURACY_COARSE表示精度低。 setSpeedAccuracy设置速度精确度。速度精确度的取值如下 ACCURACY_HIGH 精度高误差小于100米ACCURACY_MEDIUM 精度中等误差在100米到500米之间ACCURACY_LOW 精度低误差大于500米 setAltitudeRequired设置是否需要海拔信息。取值true表示需要false表示不需要。 setBearingRequired设置是否需要方位信息。取值true表示需要false表示不需要。 setCostAllowed设置是否允许运营商收费。取值true表示允许false表示不允许。 setPowerRequirement设置对电源的需求。有3个取值 Criteria.POWER_LOW 耗电低Criteria.POWER_MEDIUM 耗电中等Criteria.POWER_HIGH 耗电高 2.定位管理器 LocationManager 定位管理器用于获取定位信息的提供者、设置监听器并获取最近一次的位置信息。定位管理器的对象从系统服务LOCATION_SERVICE获取。常用方法有以下7个 getBestProvider获取最佳的定位提供者(String型数据)。第一个参数为定位条件器Criteria的实例第二个参数取值true表示只要可用的在第二参数取true时无有效定位提供者将返回null。建议第二参数参数false与监听器的onProviderDisabled和onProviderEnabled方法配合使用。定位提供者的取值(名称)说明如下 gps 卫星定位开启GPS功能network 网络定位开启数据连接或WLAN功能passive 无法定位未开启定位相关功能fused 自动选择最佳定位方法 isProviderEnabled判断指定的定位提供者是否可用。 getLastKnownLocation获取最近一次的定位地点返回值为Location对象如无有效定位信息将返回null。 requestLocationUpdates设置定位监听器。其中第一个参数为定位提供者(String型)第二个参数为位置更新的最小间隔时间第三个参数为位置更新的最小距离第四个参数为定位监听器实例。 removeUpdates移除定位监听器。 addGpsStatusListener添加定位状态的监听器。该监听器需实现GpsStatus.Listener 接口的onGpsStatusChanged方法。 removeGpsStatusListener移除定位状态的监听器。 3.定位监听器 LocationListener 定位监听器用于监听定位信息的变化事件如定位提供者的开关、位置信息发生变化等。该监听器可使用以下4种方法。onLocationChanged在位置地点发生变化时调用。在此可获取最新的位置信息。onProviderDisabled在定位提供者被用户关闭时调用。 onProviderEnabled在定位提供者被用户开启时调用。onStatusChanged在定位提供者的状态变化时调用。定位提供者的状态取值如下 OUT_OF_SERVICE 在服务范围外TEMPORARILY_UNAVAILABLE 暂时不可用AVAILABLE 可用状态 //获取权限 int iActivityCompat.checkSelfPermission(MainActivity.this,android.permission.ACCESS_FINE_LOCATION); if(i! PackageManager.PERMISSION_GRANTED){//无权限请求权限requestPermissions(new String[]{android.permission.ACCESS_FINE_LOCATION},1234); }//创建定位条件器 Criteria criterianew Criteria(); //设置精度Criteria.ACCURACY_FINE表示精确Criteria.ACCURACY_COARSE表示粗略 criteria.setAccuracy(Criteria.ACCURACY_FINE); //设置是否需要海拔信息 criteria.setAltitudeRequired(true); //设置是否需要方位信息 criteria.setBearingRequired(true); //设置是否允许运营商扣费 criteria.setCostAllowed(true); //设置对电源的需求 criteria.setPowerRequirement(Criteria.POWER_LOW);//获取定位管理者 LocationManager locationManager (LocationManager) getSystemService(LOCATION_SERVICE); //获取最佳定位提供者第二个参数表示是否只取可用的内容提供者 String locationProviderlocationManager.getBestProvider(criteria,true);//判断定位提供者是否有效 if(locationProvider!null){//设置定位监听器第一个参数为定位提供者第二个参数为最小更新时间第三个参数为最小更新距离第四个参数为定位监听器locationManager.requestLocationUpdates(locationProvider, 300, 0, new LocationListener() {//定位发生变化时触发public void onLocationChanged(NonNull Location location) {//解析Location对象中的数据}//定位提供者不可用时触发public void onProviderDisabled(NonNull String provider) {}//定位提供者可用时触发public void onProviderEnabled(NonNull String provider) {}//状态变更时触发public void onStatusChanged(String provider, int status, Bundle extras) {}});//获取最后的位置Location locationlocationManager.getLastKnownLocation(locationProvider);} else{//无有效定位提供者 } 三、解析定位信息 Location 通过LoactionManager对象使用getLastKnownLocation方法以及定位变化监听器可获取到Location型数据。 Location对象可使用以下方法解析数据 getLatitude(): 返回位置的纬度。getLongitude(): 返回位置的经度。getAltitude(): 返回位置的海拔高度如果没有海拔信息则返回 0。getAccuracy(): 返回位置的精确度。getProvider(): 返回提供位置信息的服务的名称。getTime(): 返回位置信息的时间戳。hasAltitude(): 检查位置是否包含海拔信息。hasSpeed(): 检查位置是否包含速度信息。hasBearing(): 检查位置是否包含方向信息。hasAccuracy(): 检查位置是否包含精确度信息。distanceTo(Location dest): 计算当前位置到目标位置之间的距离。bearingTo(Location dest): 计算当前位置到目标位置的方向。setLatitude(double latitude): 设置位置的纬度。setLongitude(double longitude): 设置位置的经度。setAltitude(double altitude): 设置位置的海拔高度。 textView.setText(经度location.getLongitude()纬度location.getLatitude()); 四、案例代码一览 public class MainActivity extends AppCompatActivity {private TextView textView null;private Handler handler null;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取控件textView findViewById(R.id.textView);handler new Handler(new Handler.Callback() {SuppressLint(SetTextI18n)public boolean handleMessage(NonNull Message message) {if (message.what 123) {Bundle bundle (Bundle) message.obj;//textView.setText(经度 bundle.getDouble(a1) 纬度 bundle.getDouble(a2));textView.setText(经度bundle.getDouble(a1)\n纬度bundle.getDouble(a2)\n海拔bundle.getDouble(a3)\n方向bundle.getFloat(a4)\nfrom bundle.getString(a5)\nbundle.getLong(a6)\nbundle.getString(a7));return true;}return false;}});//确认权限int i1 ActivityCompat.checkSelfPermission(MainActivity.this, android.permission.ACCESS_FINE_LOCATION);if (i1 ! PackageManager.PERMISSION_GRANTED) {//无权限申请权限requestPermissions(new String[]{android.permission.ACCESS_FINE_LOCATION}, 1);} else {//有权限beginLocation(MainActivity.this);}}SuppressLint(SetTextI18n)public void beginLocation(Context context) {//准备定位条件器Criteria criteria new Criteria();criteria.setAccuracy(Criteria.ACCURACY_FINE);//高精度criteria.setPowerRequirement(Criteria.POWER_HIGH);criteria.setCostAllowed(true);criteria.setAltitudeRequired(true);//海拔criteria.setBearingRequired(true);//方向//获取定位管理器LocationManager locationManager (LocationManager) getSystemService(LOCATION_SERVICE);//获取最佳定位提供者String provider locationManager.getBestProvider(criteria, true);if (provider ! 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// ActivityCompat#requestPermissions// here to request the missing permissions, and then overriding// public void onRequestPermissionsResult(int requestCode, String[] permissions,// int[] grantResults)// to handle the case where the user grants the permission. See the documentation// for ActivityCompat#requestPermissions for more details.return;}Log.d(OK,定位提供者为provider);locationManager.requestLocationUpdates(provider, 300, 0, new LocationListener() {Location oldLocationnull;public void onLocationChanged(NonNull Location location) {if (location ! null) {oldLocationlocation;Message message new Message();message.what 123;Bundle bundle new Bundle();bundle.putDouble(a1, location.getLongitude());bundle.putDouble(a2, location.getLatitude());bundle.putDouble(a3,location.getAltitude());bundle.putFloat(a4,location.getBearing());bundle.putString(a5,location.getProvider());bundle.putLong(a6,location.getTime());SimpleDateFormat f new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);String datef.format(location.getTime());bundle.putString(a7,date);message.obj bundle;handler.sendMessage(message);}}public void onProviderDisabled(NonNull String provider) {textView.setText(定位关停 - provider);}Overridepublic void onProviderEnabled(NonNull String provider) {textView.setText(定位启动 - provider);onLocationChanged(oldLocation);}});//获取最后结果Location locationlocationManager.getLastKnownLocation(provider);if(location!null){SimpleDateFormat f new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);String datef.format(location.getTime());textView.setText(经度location.getLongitude()\n纬度location.getLatitude()\n海拔location.getAltitude()\n方向location.getBearing()\nfrom location.getProvider()\ndate);}else{textView.setText(无定位信息);}}else {textView.setText(定位功能未开启);}}public void onRequestPermissionsResult(int requestCode, NonNull String[] permissions, NonNull int[] grantResults) {super.onRequestPermissionsResult(requestCode, permissions, grantResults);if(requestCode1){for(int i0;ipermissions.length;i){if(grantResults[i]!PackageManager.PERMISSION_GRANTED){requestPermissions(new String[]{android.permission.ACCESS_FINE_LOCATION},1);return;}}beginLocation(MainActivity.this);}} } tag定位Location基站定位WiFi定位
http://www.pierceye.com/news/82060/

相关文章:

  • 做企业网站要不要我们自己提供网站相关的图片?设计工作室与网站建设工作室
  • 陕西富通建设工程有限公司网站网站搭建有免费的吗
  • 网站服务器用什么系统公司形象墙设计制作
  • 企业网站分析案例建网站中企动力最行
  • 做seo 教你如何选择网站关键词发电机出租技术支持 东莞网站建设
  • 宣城网站seo国外产品推广平台
  • 合法购物网站建设ui培训班 qfedu
  • 成都网站建设哪个好商业街网站建设方案
  • 带平台的房子装修图片大全汕头seo全网营销
  • 如何做网站轮播大图天津滨海新区旅游景点
  • 网页制作模板的网站element怎么创建自己的博客网站
  • 如何在木上做网站关键词优化难度分析
  • 广州谷歌seo公司seo教学视频教程
  • 江苏建设执业资格注册中心官方网站电子商务网站建设与原理
  • 网站反链接什么响应式布局方案
  • 婚纱手机网站制作网站程序调试模式怎么做
  • 移动网站 模板牡丹江市建设工程交易中心网站
  • 做购物网站那个好wordpress 网站备份
  • 建设网站需要提交什么资料wordpress 用什么编辑器
  • 电子商务网站的建设wordpress可视化插件
  • 深圳航空公司官方网站招聘安卓做网站
  • 建设各网站需要多久网站上的动态图怎么做
  • seo网站设计什么平台打广告比较好免费的
  • 涡阳网站建设万国企业网
  • 定制网站和模板建站哪个好用怎样创建网站信息平台
  • 珠海网站建设创意个人网站可以备案几个
  • 网站稳定期怎么做logo免费设计软件
  • 做自媒体小视频哪个网站比较赚钱深圳市浩天建设网站
  • 知名商城网站建设多少钱天津电力建设公司招标网站
  • 深圳做门户网站市场监督管理局投诉举报管理办法