您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關如何在android應用中利用service實現一個計時器功能,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
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>
以上就是如何在android應用中利用service實現一個計時器功能,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。