简体   繁体   English

如何在Android中使用AlarmManager创建自定义日历并更新日期/时间

[英]How to create custom Calendar and update date/time with AlarmManager in Android

Like Digital clock or Analog Clock, Do we have anything to display the date inside of our application. 像数字时钟或模拟时钟一样,我们是否可以在应用程序内部显示日期? This should change the date on every day which is similar to digital clock changes time for every second. 这应该每天更改日期,这类似于数字时钟每秒更改时间。

If there is no controller is available then how can we display the date inside of our app. 如果没有可用的控制器,那么我们如何在应用程序内部显示日期。 And this should change whenever the mobile date changes. 每当移动日期更改时,这都应该更改。

Please help me on this. 请帮我。

You may need timer for this as follows , 您可能需要以下计时器,

  1. Initialize Timer,TimerTask and handler 初始化Timer,TimerTask和处理程序

     Timer timer; TimerTask timerTask; final Handler handler = new Handler(); 
  2. Start timer within OnResume() and will repeat once a minute 在OnResume()中启动计时器,并将每分钟重复一次

     @Override protected void onResume() { super.onResume(); timer = new Timer(); timer.schedule(timerTask, 0, 60000); } 
  3. Display the time 显示时间

     public void setUpTimer() { timerTask = new TimerTask() { public void run() { handler.post(new Runnable() { public void run() { SimpleDateFormat sdf = new SimpleDateFormat(""EEE, d MMM yyyy HH:mm:ss Z""); String date = sdf.format(new Date()); textView.setText(date); } }); } }; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM