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>
好了,一个简单的计时就完成了,写的不好请见谅!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)