位置: 编程技术 - 正文

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

  • 跨年的所得税会计分录
  • 利息收入需要交印花税吗
  • 其他应收款计提坏账吗
  • 即征即退办理时限是多少
  • 供货商对账单跨月返利怎么做账?
  • 银行承兑汇票怎么填写
  • 收到投资者购买股票的资金
  • 增值税欠缴税会不会影响企业的信用
  • 转账支票给员工转工资
  • 外地建安个人所得税标准
  • 小规模定期定额计税依据
  • 工程个人所得税扣除标准是多少
  • 个体户需要缴纳残疾人保障金吗
  • 技术先进型企业认定
  • 收到押金入什么会计科目
  • 房地产企业城市配套费
  • 个人所得税代扣代缴的适用范围
  • 电脑开始菜单在右边怎么调回来
  • 赠品怎么做账务分录
  • 净资产有哪些科目
  • 防疫物资费用会计处理
  • 怎么给自己的宽带改名字
  • 苹果手机下载手机克隆哪个版本
  • 捐赠固定资产入账价值
  • win11资源管理器卡死
  • 大模型训练成本
  • 强化税收风险意识
  • 运输发票的抵扣税率
  • php官方文档
  • 工程项目成本费用的分类有哪些?
  • 债券转换是什么意思
  • php pdo sqlserver
  • vue叠化在哪里
  • 基于java的电子书店管理系统
  • 回顾2021年作文
  • cp命令复制文件到另一个目录并改名
  • 核销发生的坏账损失
  • 织梦图集的使用教程
  • 如何在控制台打印debug里的数据
  • 差旅费可以抵扣嘛
  • 事业单位结余分配的去向有
  • 营业税金及附加计入什么科目
  • 首次购买金税盘怎么做账
  • 什么叫递延收益和其他收益
  • 建筑业发票可以抵扣制造业进项
  • 企业盘亏的设备会计分录
  • 收到劳务公司代发工资怎么做账
  • 利息收入的正确分录凭证怎么写
  • 去年的物业费今年收到了可以确认收入吗
  • 未打印税务分类怎么处理
  • 开发票时如何添加商品编码?
  • 服务费的进项发票怎么做分录
  • 使用u盘安装windows10电脑识别不出来
  • 教大家如何重做作业
  • award bios设置图解教程
  • windows xp玩游戏
  • xpkw
  • 苹果mac怎么复制文字
  • rundull32.exe
  • win8系统怎么设置桌面图标
  • win7鼠标突然不能用了
  • 怎么免费升级win10系统
  • windows8中文版是什么版本
  • win10预览版21301bug
  • GLSL Tessellation Shader的编程入门介绍
  • [置顶]bilinovel
  • 苹果手机如何给视频添加字幕
  • 解读css发展历史简述
  • django 实例
  • OSG 砖块 shader 例子 GLSL
  • 微信小程序简单的音乐播放器
  • nodejs图片合成
  • 字符串查找子串
  • python jose
  • JavaScript获取网页内容
  • 仿百度首页html代码静态
  • 手把手教你打造班级主题环境 培训后心得
  • js弹出对话框的命令
  • 税控盘过了时间没清卡要罚多少钱
  • 石家庄经开区税务局
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设