Android studio日历与时钟,秒级更新

package com.example.scm;

import android.annotation.SuppressLint;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

import static android.os.SystemClock.sleep;

public class CalendarTime {
    @SuppressLint("SimpleDateFormat")
    public static SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static Date date;
    public static String dayandtime=simpleDateFormat.format(date);
    public static TextView TitleTimeText;
    //使用前请绑定控件
    //TitleTimeText = findViewById(R.id.title_2_time);
    public CalendarTime() {
        new Thread(() -> {
            while (true) {
                CalendarTime.date=new Date(System.currentTimeMillis());
                dayandtime=simpleDateFormat.format(date);
               //1 TitleTimeText.setText(CalendarTime.dayandtime);
                sleep(1000);
            }
        }).start();
    }
}

思路:1个秒级更新线程,获取和显示每秒一次,达到秒级更新。

2.使用比较灵活,可以直接主程序将线程一块copy过去就行,然后绑定控件,写控件都在线程完成。

3.子线程中更新UI是不安全 的,所以待改为线程间通信,让UI线程去处理界面UI的更新。当然,android studio中它还是可以运行的,但是会报线程不安全异常。应该改为:

package com.example.scm;

import android.annotation.SuppressLint;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

import static android.os.SystemClock.sleep;

public class CalendarTime {
    @SuppressLint("SimpleDateFormat")
    public static SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static Date date;
    public static String dayandtime=simpleDateFormat.format(date);
    //public static TextView TitleTimeText;
    //使用前请绑定控件
    //TitleTimeText = findViewById(R.id.title_2_time);
    public CalendarTime() {
        new Thread(() -> {
            while (true) {
                CalendarTime.date=new Date(System.currentTimeMillis());
                dayandtime=simpleDateFormat.format(date);
                    Message msg=new Message();
                    msg.arg1=0x1000;
                    //msg.obj=dayandtime;
                 if(MainActivity.handle!=null)
                   MainActivity.handle.sendMessage(msg);
               //1 TitleTimeText.setText(CalendarTime.dayandtime);
                sleep(1000);
            }
        }).start();
    }
}

主mainactivity中收:

CalendarTime calendartime=new CalendarTime();//启动日历时间对象像线程
Handler handle=new Handler(){
public void handleMessage(Message msg){
if(msg.arg1==0x1000)
  textview.setText(CalendarTime.dayandtime);//接收设置数据
}
}
如果handle里面的数据太多,可以标注为弱参数,让GC及时处理。

这样就没有隐患了。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Android Studio 提供了强大的开发工具和功能,可以帮助开发者创建和管理日历应用程序。对于日历功能,Android Studio 使用了 Android Calendar Provider API 以及相关的类和方法。 实现一个日历应用程序需要以下步骤: 1. 添加权限:在 AndroidManifest.xml 文件中添加以下权限: ``` <uses-permission android:name="android.permission.READ_CALENDAR" /> <uses-permission android:name="android.permission.WRITE_CALENDAR" /> ``` 2. 创建日历事件: 使用 ContentResolver 类和相关方法向系统日历中添加事件。例如,可以使用以下代码创建一个日历事件: ```java ContentResolver cr = getContentResolver(); ContentValues values = new ContentValues(); values.put(CalendarContract.Events.DTSTART, startMillis); values.put(CalendarContract.Events.DTEND, endMillis); values.put(CalendarContract.Events.TITLE, "Event Title"); values.put(CalendarContract.Events.DESCRIPTION, "Event Description"); values.put(CalendarContract.Events.CALENDAR_ID, calID); values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone); Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values); ``` 3. 查询日历事件: 使用 ContentResolver 类和相关方法从系统日历中查询事件。例如,可以使用以下代码查询今天的日历事件: ```java ContentResolver cr = getContentResolver(); Calendar calendar = Calendar.getInstance(); long startOfDay = calendar.getTimeInMillis(); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); long endOfDay = calendar.getTimeInMillis(); Uri uri = CalendarContract.Events.CONTENT_URI; String[] projection = {CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART}; String selection = "(" + CalendarContract.Events.DTSTART + ">=? AND " + CalendarContract.Events.DTSTART + "<=?)"; String[] selectionArgs = {String.valueOf(startOfDay), String.valueOf(endOfDay)}; String sortOrder = CalendarContract.Events.DTSTART + " ASC"; Cursor cursor = cr.query(uri, projection, selection, selectionArgs, sortOrder); while (cursor.moveToNext()) { String title = cursor.getString(cursor.getColumnIndex(CalendarContract.Events.TITLE)); long startTime = cursor.getLong(cursor.getColumnIndex(CalendarContract.Events.DTSTART)); // 处理查询到的日历事件数据 } cursor.close(); ``` 这些是简单的示例,实际开发中还可以根据需求进行更多操作,比如更新和删除日历事件等。 希望以上信息对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值