欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android利用service完成计时功能

程序员文章站 2023-12-01 08:21:04
本文为大家分享了android service计时功能的具体代码,供大家参考,具体内容如下 源码下载地址:https://github.com/luoye123/ti...

本文为大家分享了android service计时功能的具体代码,供大家参考,具体内容如下

android利用service完成计时功能

源码下载地址:https://github.com/luoye123/timing

1、首先建立主页面的设计:activity_time.xml

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/self_driving_wait_ll"
 android:layout_width="match_parent"
 android:layout_height="match_parent">


 <linearlayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:gravity="center"
 android:orientation="vertical">

 <textview
  android:id="@+id/tv_time"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerinparent="true"
  android:layout_margintop="20.0dp"
  android:gravity="center_horizontal"
  android:text="00:00:00"
  android:textsize="88.0sp" />
 </linearlayout>
</relativelayout>

2、activity的建立:timeactivity

public class timeactivity extends appcompatactivity {

 public static string time_changed_action = "com.yy.time.time_changed_action";
 public static textview tv_time;
 private sharedpreferencesutil util;

 @override
 public void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_time);
 tv_time= (textview) findviewbyid(r.id.tv_time);
 util=new sharedpreferencesutil(this);
 util.savestring(mycontant.starttime, stringutils.gettime());
 startservice(new intent(this, timeservice.class));

 }
}

3、service的建立:timeservice

public class timeservice extends service {
private string tag = "timeservice";
private timer timer = null;
private intent timeintent = null;
private sharedpreferencesutil util;

@override
public void oncreate() {
 super.oncreate();
 log.i(tag,"timeservice->oncreate");
 //初始化
 this.init();
 //定时器发送广播
 timer.schedule(new timertask() {
 @override
 public void run() {
  //发送广播
  sendtimechangedbroadcast();
 }
 }, 1000,1000);
}
@override
public ibinder onbind(intent intent) {
 log.i(tag,"timeservice->onbind");
 return null;
}
/**
 * 相关变量初始化
 */
private void init(){
 util=new sharedpreferencesutil(this);
 timer = new timer();
 timeintent = new intent();
}

/**
 * 发送广播,通知ui层时间已改变
 */
private void sendtimechangedbroadcast(){
 try {
 timeintent.putextra("time",gettime());
 timeintent.setaction(timeactivity.time_changed_action);
 //发送广播,通知ui层时间改变了
 sendbroadcast(timeintent);
 } catch (parseexception e) {
 e.printstacktrace();
 }
}
/**
 * 获取最新时间
 * @return
 */
private string gettime() throws parseexception {

 string time;
 time=getsubtract(util.readstring(mycontant.starttime));
 return time;
}

//时间相减 得到计时时间
public string getsubtract(string starttime) throws parseexception {

 simpledateformat myformatter = new simpledateformat( "hh:mm:ss");
 string newtime= stringutils.gettime();
 date date= myformatter.parse(newtime);
 date mydate= myformatter.parse(starttime);
 int sec= (int) ((date.gettime()-mydate.gettime())/1000);
 int min=sec/60;
 int hour=min/60;

 if (sec >= 60) {
 sec = (sec % 60);
 }

 if (min >= 60) {
 min = (min % 60);
 }
 string hstring;
 string mstring;
 string string;
 if (hour < 10) {
 hstring = "0" + string.valueof(hour);
 } else {
 hstring = string.valueof(hour);
 }
 if (min < 10) {
 mstring = "0" + string.valueof(min);
 } else {
 mstring = string.valueof(min);
 }
 if (sec < 10) {
 string = "0" + string.valueof(sec);
 } else {
 string = string.valueof(sec);
 }

 return hstring + ":" + mstring + ":" + string;
}

@override
public componentname startservice(intent service) {
 log.i(tag,"timeservice->startservice");
 return super.startservice(service);
}

@override
public void ondestroy() {
 super.ondestroy();
 log.i(tag,"timeservice->ondestroy");
}
}

4、broadcastreceiver广播接受者,更新ui界面的时间:uitimereceiver

public class uitimereceiver extends broadcastreceiver {
private timeactivity duiactivity = new timeactivity();
@override
public void onreceive(context context, intent intent) {
 string action = intent.getaction();
 if(timeactivity.time_changed_action.equals(action)){
 string strtime = intent.getstringextra("time");
 //此处实现不够优雅,为了在uitimereceiver中使用dynamicuiactivity中的textview组件time,而将其设置为public类型,
 //更好的实现是将uitimereceiver作为dynamicuiactivity的内部类
 duiactivity.tv_time.settext(strtime);
 }
}
}

5、记住要在配置文件里面配置哦!

 <service android:name=".service.timeservice"/>
 <receiver android:name=".service.uitimereceiver">
 <intent-filter>
  <action android:name="com.yy.time.time_changed_action"/>
 </intent-filter>
 </receiver>

好了,一个简单的计时就完成了,写的不好请见谅!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。