Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How can Android synchronize the calendar of the system

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how Android can synchronize the calendar of the system. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Permissions

2. Dynamic permission Activity

If (ContextCompat.checkSelfPermission (this, Manifest.permission.WRITE_CALENDAR)! = PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions (this, new String [] {Manifest.permission.WRITE_CALENDAR, Manifest.permission.READ_CALENDAR}, 1);}

3. Use

String tipTime = "2021-04-25 12:00"; long start = TimeUtils.stringToLong (tipTime, "yyyy-MM-dd HH:mm"); long end = start + 60 * 60 * 1000L; LogUtil.d ("start:" + start); LogUtil.d ("end:" + end); String [] split = tipTime.split (""); String [] dateStr = split [0] .split ("-") Int month = Integer.parseInt (dateStr [1]); int day = Integer.parseInt (dateStr [2]); String until = "20221231T235959Z"; / / cut-off date: 23:59:59 on December 31, 2022 String mRule = ""; switch (mRepeatType) {case 1 mRepeatType / do not repeat mRule = null; break Case 2FREQ=YEARLY;UNTIL= FREQ=YEARLY;UNTIL= / annual repeat (a certain day of the year) mRule = "FREQ=YEARLY;UNTIL=" + until + "; WKST=SU;BYMONTH=" + month + "; BYMONTHDAY=" + day; break; case 3 until + "/ monthly (some day of the month) mRule =" FREQ=MONTHLY;UNTIL= "+ until +"; WKST=SU;BYMONTHDAY= "+ day; break Case 4split / repeat weekly String date = split [0]; String week = TimeUtils.getWeek (date); switch (week) {case "Sunday": mRule = "FREQ=WEEKLY;UNTIL=" + until + "; WKST=SU;BYDAY=SU"; break Case "Monday": mRule = "FREQ=WEEKLY;UNTIL=" + until + "; WKST=SU;BYDAY=MO"; break; case "Tuesday": mRule = "FREQ=WEEKLY;UNTIL=" + until + "; WKST=SU;BYDAY=TU"; break Case "Wednesday": mRule = "FREQ=WEEKLY;UNTIL=" + until + "; WKST=SU;BYDAY=WE"; break; case "Thursday": mRule = "FREQ=WEEKLY;UNTIL=" + until + "; WKST=SU;BYDAY=TH"; break Case "Friday": mRule = "FREQ=WEEKLY;UNTIL=" + until + "; WKST=SU;BYDAY=FR"; break; case "Saturday": mRule = "FREQ=WEEKLY;UNTIL=" + until + "; WKST=SU;BYDAY=SA"; break } break; case 5until; break; case mRule / per day (daily) mRule = "FREQ=WEEKLY;UNTIL=" + until + "; WKST=SU;BYDAY=" String [] weeks = mSelectedWeekStr.split (","); for (int I = 0; I

< weeks.length; i++) { if (weeks[i].equals("周日")) { mRule += "SU,"; } else if (weeks[i].equals("周一")) { mRule += "MO,"; } else if (weeks[i].equals("周二")) { mRule += "TU,"; } else if (weeks[i].equals("周三")) { mRule += "WE,"; } else if (weeks[i].equals("周四")) { mRule += "TH,"; } else if (weeks[i].equals("周五")) { mRule += "FR,"; } else if (weeks[i].equals("周六")) { mRule += "SA,"; } } if (mRule.endsWith(",")) { mRule.substring(0, mRule.length() - 1); } break; } LogUtil.d("mRule:" + mRule); CalendarEvent calendarEvent = new CalendarEvent( mContent, mContent, "", start, end, 0, mRule ); // 添加事件int addResult = CalendarProviderManager.addCalendarEvent(AddScheduleActivity.this, calendarEvent);if (addResult == 0) { ToastUtil.showShortToast("插入成功"); } else if (addResult == -1) { ToastUtil.showShortToast("插入失败"); } else if (addResult == -2) { ToastUtil.showShortToast("没有权限"); } 3、时间转换方法 public static long stringToLong(String strTime, String formatType) { Date date = null; // String类型转成date类型 try { date = stringToDate(strTime, formatType); } catch (Exception e) { e.printStackTrace(); } if (date == null) { return 0; } else { long currentTime = dateToLong(date); // date类型转成long类型 return currentTime; } } public static Date stringToDate(String strTime, String formatType) { SimpleDateFormat formatter = new SimpleDateFormat(formatType); Date date = null; try { date = formatter.parse(strTime); } catch (ParseException e) { e.printStackTrace(); } return date; } // date要转换的date类型的时间 public static long dateToLong(Date date) { return date.getTime(); } 4、CalendarProviderManager 类 import android.content.ContentUris;import android.content.ContentValues;import android.content.Context;import android.content.Intent;import android.content.pm.PackageManager;import android.database.Cursor;import android.graphics.Color;import android.net.Uri;import android.os.Build;import android.provider.CalendarContract; import calendarprovider.Util; import java.util.ArrayList;import java.util.List;import java.util.TimeZone; import static com.kyle.calendarprovider.Util.checkContextNull; /** * 系统日历工具 */public class CalendarProviderManager { private static StringBuilder builder = new StringBuilder(); /** * TIP: 要向系统日历插入事件,前提系统中必须存在至少1个日历账户 ** / // ----------------------- 创建日历账户时账户名使用 --------------------------- private static String CALENDAR_NAME = "TestC"; private static String CALENDAR_ACCOUNT_NAME = "Test"; private static String CALENDAR_DISPLAY_NAME = "Test的账户"; // ------------------------------- 日历账户 ----------------------------------- /** * 获取日历账户ID(若没有则会自动创建一个) * * @return success: 日历账户ID failed : -1 permission deny : -2 */ @SuppressWarnings("WeakerAccess") public static long obtainCalendarAccountID(Context context) { long calID = checkCalendarAccount(context); if (calID >

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report