位置: 编程技术 - 正文

android中定时定位的实现(android定时器的使用)

编辑:rootadmin
想要实现这个功能,应该分为三部分工作。(1)定时功能的实现,调用系统当前时间,使用timer实现定时功能;(2)当前位置的获取,使用GPS功能获取当前位置,并且显示出经度和纬度,并且使用位置监听事件LocationListener监听位置变化,当位置改变时,获取新的经纬度,并显示;(3)将两部分功能进行整合为一个整体。代码如下:import java.text.SimpleDateFormat;import java.util.Date;import java.util.Timer;import java.util.TimerTask;import android.location.Criteria;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.annotation.SuppressLint;import android.app.Activity;import android.appwidget.AppWidgetManager;import android.content.Context;import android.text.method.DateTimeKeyListener;import android.view.Menu;import android.widget.TextView;import android.widget.Toast;public class TimerActivity extends Activity {private Handler handler = new Handler();Timer timer;Timer timerone;TimerTask task = new TimerTask() {// 任务public void run() {Message message = new Message();message.what = 1;handler.sendMessage(message);}};TimerTask taskone = new TimerTask() {// 任务public void run() {Message message = new Message();message.what = 0;handler.sendMessage(message);}};String g_latLongString;@SuppressLint("HandlerLeak")@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);timer = new Timer(true);timer.schedule(task, 0, ); // 延时ms后执行,ms执行一次timerone = new Timer(true);timerone.schedule(taskone, 0, ); // 延时ms后执行,ms执行一次// timer.cancel(); //退出计时器// 通过系统服务,取得LocationManager对象//声明LocationManager对象 LocationManager loctionManager; String contextService=Context.LOCATION_SERVICE; //通过系统服务,取得LocationManager对象 loctionManager=(LocationManager) getSystemService(contextService); //通过GPS位置提供器获得位置// String provider=LocationManager.GPS_PROVIDER;// Location location = loctionManager.getLastKnownLocation(provider); //使用标准集合,让系统自动选择可用的最佳位置提供器,提供位置 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 = loctionManager.getBestProvider(criteria, true); //获得最后一次变化的位置 Location location = loctionManager.getLastKnownLocation(provider); //显示在TextView中 updateWithNewLocation(location); //监听位置变化,2秒一次,距离米以上 loctionManager.requestLocationUpdates(provider, , 2, locationListener);handler = new Handler() {// 委托@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case 1:try {TextView recordLoctionText = (TextView)findViewById(R.id.recordLoctionText);recordLoctionText.append(g_latLongString &#; "n");} catch (Exception e) {Toast.makeText(getApplicationContext(), e.toString(),Toast.LENGTH_SHORT).show();}break;case 0:SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");Date curDate = new Date(System.currentTimeMillis());// 获取当前时间String str = formatter.format(curDate);TextView mytimeText = (TextView)findViewById(R.id.mytimeText);mytimeText.setText(str );}}};}//位置监听器 private final LocationListener locationListener = new LocationListener() {@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {}@Overridepublic void onProviderEnabled(String provider) {}@Overridepublic void onProviderDisabled(String provider) {updateWithNewLocation(null);}//当位置变化时触发@Overridepublic void onLocationChanged(Location location) {//使用新的location更新TextView显示updateWithNewLocation(location);}}; // 将位置信息显示在TextView中private void updateWithNewLocation(Location location) {String latLongString;TextView myLoctionText;myLoctionText = (TextView) findViewById(R.id.myLoctionText);if (location != null) {double lat = location.getLatitude();double lng = location.getLongitude();latLongString = "Lat(纬度): " &#; lat &#; "nLong(经度): " &#; lng;g_latLongString = latLongString;} else {latLongString = "没找到位置";}// SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// Date curDate = new Date(System.currentTimeMillis());// 获取当前时间// String str = formatter.format(curDate);myLoctionText.setText( "您当前的位置是:n" &#; latLongString);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.timer, menu);return true;}}XML文件:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" ><TextView android:id="@&#;id/mytimeText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:textSize="dip" /> <TextView android:id="@&#;id/myLoctionText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:textSize="dip" /> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="none" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="dp" android:orientation="vertical" > <TextView android:id="@&#;id/recordLoctionText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:fadeScrollbars="false" android:scrollbars="vertical" android:text="" android:textColor="#" android:textSize="dip" /> </LinearLayout> </ScrollView></LinearLayout>

推荐整理分享android中定时定位的实现(android定时器的使用),希望有所帮助,仅作参考,欢迎阅读内容。

android中定时定位的实现(android定时器的使用)

文章相关热门搜索词:android 定时器,android 定时执行,android定时任务是什么,android 定时器,安卓app自动定时执行操作,安卓app自动定时执行操作,android定时执行某个任务,android定时任务是什么,内容如对您有帮助,希望把文章链接给更多的朋友!

Android之Volley 简介:Volley是GoogleI/O上Google官方发布的一款Android平台上的网络通信库。以前的网络请求,要考虑开启线程、内存泄漏、性能等等复杂的问题。但是Vol

android中注册页面实现 自己动手做的第一个demo,简单的注册页面的实现,并且注册成功后返回注册信息,适用于android新手基本控件的使用。注册页面的实现:importandroid.os.Bund

Android酷炫实用的开源框架(UI框架)(转载,只为保存) 转自Android开发中文站»Android酷炫实用的开源框架(UI框架),点击打开链接。转载该博客,只为保存,留下自己慢慢体验。1.Side-Menu.Android分类侧滑菜单

标签: android定时器的使用

本文链接地址:https://www.jiuchutong.com/biancheng/374481.html 转载请保留说明!

上一篇:普通本科生面试总结以及在校编程经历(本科生面试的自我介绍)

下一篇:Android之Volley

  • 公司名下的车怎么报废
  • 残疾人加计扣除政策文件
  • 绿化税票多少税率
  • 税务申报有什么技巧
  • 税后是含税还是不含税的意思
  • 非盈利组织是两套账吗?
  • 分公司非独立核算是什么意思
  • 机打发票报销有效期
  • 印花税按什么征收
  • 出口货物退回需要进口税吗
  • 免税和不征税货一样吗
  • 报废车辆补贴收据怎么写
  • 水电开发潜力
  • 工程预算调整的主要内容
  • 残保金和工会经费需要计提吗
  • 小规模纳税人减按1%如何填报申报表
  • 专票电话写错了怎么办
  • 城市维护建设税减免税优惠政策
  • 1697510703
  • 党费返还款怎么做分录
  • 如何看云空间的内容
  • 出口退税新政策对出口贸易的影响
  • 财政补贴会计入养老金一起发放吗
  • 怎么找回windows图片查看器
  • 信号差的要死
  • 房地产企业哪些成本上升了
  • 加速折旧法计算公式 CFA
  • php常见面试题
  • 计提坏账准备对所得税费用的影响
  • 阿德莱德 景点
  • 信用减值损失属于损益类科目吗
  • 前端的基本知识
  • php+ mysql教程
  • 2021前端面试大全
  • 若依框架前端改造
  • 我初次尝试制作的英文怎么写
  • 红包生成算法
  • 无形资产法
  • 印花税土地使用权计税依据
  • 管理费用属于什么类
  • 购进固定资产的进项税可以抵扣吗
  • python删除列表的方法
  • 高新技术企业相关税收政策
  • mysql5.7性能优化
  • 转让股权个税的计税基础
  • 负数发票开票条件?
  • 账龄划分中有借有贷怎么分析
  • 负数发票作废后对原发票有影响吗
  • 营销策划费用是
  • 其他综合收益要转入投资收益吗
  • 水泥销售技巧
  • 金税盘每年服务费可以抵扣吗
  • mysql数据库-数据库和表的基本操作
  • mysql中汉字用什么类型
  • mac系统操作教程
  • win7登录不了系统界面
  • win10如何设置视频缩略图
  • window终端是什么
  • 巧用护手霜保养皮衣
  • 双系统怎么设置引导启动项
  • centos7如何扩展根分区空间
  • mac系统怎么设置字体大小
  • windows mobile10
  • windows10的操作中心
  • win8.1如何更新到win10
  • perl中splice
  • cocos 源码
  • python accdb
  • dos命令检测硬盘坏道
  • 批量创建用户
  • centos创建shell脚本
  • python3使用PyMysql连接mysql数据库实例
  • 瀑布流软件
  • android DefaultHttpClient设置setCookieStore
  • 关于javascript函数
  • Python 制作糗事百科爬虫实例
  • 湖南国家税务局网上电子税务局官网
  • 税务询问笔录属于什么证据
  • 重庆市怎么在网上打印章程
  • 税务局看场地要给红包吗
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设