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

怎样开网站如何快速推广一个新产品

怎样开网站,如何快速推广一个新产品,代办集团公司注册,wordpress url地址毫无疑问,要想使用LocationManager就必须要先获取到它的实例,我们可以调用Context的getSystemService()方法获取到。getSystemService()方法接收一个字符串参数用于确定获取系统的哪个服务,这里传入Context.LOCATION_SERVICE即可。因此&#…

  毫无疑问,要想使用LocationManager就必须要先获取到它的实例,我们可以调用Context的getSystemService()方法获取到。getSystemService()方法接收一个字符串参数用于确定获取系统的哪个服务,这里传入Context.LOCATION_SERVICE即可。因此,获取LocationManager的实例就可以写成: 

LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

Android 中一般有三种位置 提供器可供选择,GPS_PROVIDER、NETWORK_PROVIDER和PASSIVE_PROVIDER。其中前两种使用的比较多,分别表示使用GPS定位和使用网络定位。这两种定位方式各有特点,GPS定位的精准度比较高,但是非常耗电,而网络定位的精准度稍差,但耗电量比较少。 我们应该根据自己的实际情况来选择使用哪一种位置提供器,当位置精度要求非常高的时候,最好使用GPS_PROVIDER,而一般情况下,使用NETWORK_PROVIDER会更加得划算。 

 将选择好的位置提供器传入到getLastKnownLocation()方法中,就可以得到一个Location对象,如下所示: 

String provider = LocationManager.NETWORK_PROVIDER; Location location = locationManager.getLastKnownLocation(provider); 
 这个Location对象中包含了经度、纬度、海拔等一系列的位置信息,然后从中取出我们所关心的那部分数据即可。 
 如果有些时候你想让定位的精度尽量高一些,但又不确定GPS定位的功能是否已经启用,这个时候就可以先判断一下有哪些位置提供器可用,如下所示: 

List<String> providerList = locationManager.getProviders(true);

 可以看到,getProviders()方法接收一个布尔型参数,传入true就表示只有启用的位置提供器才会被返回。之后再从providerList中判断是否包含GPS定位的功能就行了。 

 另外,调用getLastKnownLocation()方法虽然可以获取到设备当前的位置信息,但是用户是完全有可能带着设备随时移动的,那么我们怎样才能在设备位置发生改变的时候获取到最新的位置信息呢?不用担心,LocationManager还提供了一个requestLocationUpdates()方法,只要传入一个LocationListener的实例,并简单配置几个参数就可以实现上述功能了,写法如下: 

mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, new LocationListener() {@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {// TODO Auto-generated method stub}@Overridepublic void onProviderEnabled(String provider) {// TODO Auto-generated method stub}@Overridepublic void onProviderDisabled(String provider) {// TODO Auto-generated method stub}@Overridepublic void onLocationChanged(Location location) {// TODO Auto-generated method stub}});

  这里requestLocationUpdates()方法接收四个参数,第一个参数是位置提供器的类型,第二个参数是监听位置变化的时间间隔,以毫秒为单位,第三个参数是监听位置变化的距离间隔,以米为单位,第四个参数则是LocationListener监听器。这样的话,LocationManager每隔5秒钟会检测一下位置的变化情况,当移动距离超过10米的时候,就会调用LocationListener的onLocationChanged()方法,并把新的位置信息作为参数传入。 

整个例子的代码如下所示

package com.test.locationtest;import java.util.List;import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends Activity {private TextView tvLocation;private LocationManager locationManager;private String provider;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tvLocation = (TextView) findViewById(R.id.tv_location);locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);List<String> allProvider = locationManager.getProviders(true);if (allProvider.contains(LocationManager.GPS_PROVIDER)) {provider = LocationManager.GPS_PROVIDER;} else if (allProvider.contains(LocationManager.NETWORK_PROVIDER)) {provider = LocationManager.NETWORK_PROVIDER;} else {Toast.makeText(this, "请打开GPS 或者 数据", Toast.LENGTH_SHORT).show();return;}Location location = locationManager.getLastKnownLocation(provider);Log.d("MainActivity", "显示位置");showLocation(location);}LocationListener locationListener = new LocationListener() {@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {// TODO Auto-generated method stub}@Overridepublic void onProviderEnabled(String provider) {// TODO Auto-generated method stub}@Overridepublic void onProviderDisabled(String provider) {// TODO Auto-generated method stub}@Overridepublic void onLocationChanged(Location location) {showLocation(location);}};@Overrideprotected void onDestroy() {super.onDestroy();if (locationManager != null) {// 关闭程序时将监听器移除locationManager.removeUpdates(locationListener);}}private void showLocation(Location location) {//这里获取location 可能为空  while (location == null) {locationManager.requestLocationUpdates(provider, 2000, (float) 0.1,locationListener);}if (location != null) {String currentPosition = "latitude is " + location.getLatitude()+ "\n" + "longitude is " + location.getLongitude();Log.d("MainActivity", currentPosition);tvLocation.setText(currentPosition);}}
}

获取Location 可能为空 ,可以自己百度解决下


还需要增加以下权限

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android:name="android.permission.INTERNET"/>




http://www.tj-hxxt.cn/news/87487.html

相关文章:

  • 做民宿加盟哪些网站比较好百度seo和sem
  • 做网站需要公司么百度免费优化
  • 采网站建设百度问答怎么赚钱
  • 做网站挂靠服务器什么好谷歌seo排名优化服务
  • 广东两学一做网站北京优化网站推广
  • 网站建设公司网站制作金华百度推广公司
  • 怎么才能搜索到自己做的网站厦门seo计费
  • 济南网站推广建设有限公司seo模拟点击软件
  • 萌宝宝投票网站怎么做网络推广最好的网站有哪些
  • 秦皇岛庆云网站建设网络公司网站
  • 旅游公司网站开发网站服务器一年的费用
  • 新开网站seo网站域名查询工具
  • 包头网站开发策划公司排行榜
  • 网站免费空间免备案公司调查公司
  • wordpress跳转到子页面百度搜索引擎优化
  • 专门找图片素材的网站情感营销案例
  • 三亚网站建设百度快照如何优化
  • 织梦网站怎么做伪静态页面seo关键词
  • 网站 锚点链接怎么做新东方教育培训机构
  • 手机复制链接提取视频的软件百度禁止seo推广
  • 大学 建网站百度游戏
  • 深圳网站制作大运软件小镇网站搜索排名优化软件
  • 东营网站推广排名百度网页版登录入口
  • 公司发布网站需要备案河北百度seo软件
  • 武汉模板建站定制网站市场营销策划公司
  • 网站怎么做才济南优化哪家好
  • 网站开发工具简述刷神马网站优化排名
  • 网站什么认证对做电商好建站之星官方网站
  • 有哪些免费做简历的网站国外网站推广
  • 上海包装设计公司有哪些赣州seo排名