Android Studio 基础 之 获取系统Calendar 日历日程 (涉及指定日期时间判断是星期几的方法使用)的方法整理
Android Studio 基础 之 获取系统Calendar 日历日程 (涉及指定日期时间判断是星期几的方法使用)的方法整理
目录
Android Studio 基础 之 获取系统Calendar 日历日程 (涉及指定日期时间判断是星期几的方法使用)的方法整理
一、简单介绍
Android 开发中的一些基础操作,使用整理,便于后期使用。
本节介绍,Android 开发中,获取手机设备的系统Calendar 的 日历日程的方法,方法不唯一,欢迎指正。
二、实现原理
1、getContentResolver().query(Uri.parse(CALENDER_EVENT_URL), null, null, null, "dtstart"+" DESC");
获取日历日程事件 Cursor,然后循环获取每个日程事件
2、getContentResolver().query(Uri.parse(CALENDER_EVENT_URL), null, null, null, "dtstart"+" DESC");
"dtstart"+" DESC",实现日历日程事件按日程事件开始时间降序排序得到日程事件结果
3、Cursor.getString(eventCursor.getColumnIndex("title")) 得到日程事件标题
4、Cursor.getString(eventCursor.getColumnIndex("description")) 得到日程事件描述
5、Cursor.getString(eventCursor.getColumnIndex("eventLocation")) 得到日程事件位置
6、Cursor.getString(eventCursor.getColumnIndex("dtstart")) 得到日程事件开始时间
7、Cursor.getString(eventCursor.getColumnIndex("dtend")) 得到日程事件结束时间
8、Calendar.getInstance().setTime(format.parse("yyyy-MM-dd HH:mm:ss")) 判断指定日期时间是星期几
三、注意实现
1、添加Calendar 相关权限
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
2、getContentResolver().query() 最后一个参数进行数据排序,这里是 "dtstart"+" DESC" 日程事件开始时间降序排序, "dtstart"+" AXSC" 日程事件开始时间升序排序
四、效果预览
五、实现步骤
1、打开 Android Studio 新建一个工程,或者新建一个模块
2、默认操作,一路创建一个模块
3、创建一个 calendarUtil,编写获取日程日志事件的方法,CalenderDataStruct 日历日程事件的结构体
4、MainActivity ,测试 calendarUtil封装的方法
5、打包,在设备上测试,效果如上
六、关键代码
1、CalenderDataStruct
package com.example.calendartest;
/**
* 日历日程数据结构
*/
public class CalenderDataStruct {
String eventTitle = "";
String startTime = "";
String endTime = "";
String description = "";
String location = "";
String week = "";
public CalenderDataStruct(String eventTitle, String startTime, String endTime, String description, String location,String week) {
this.eventTitle = eventTitle;
this.startTime = startTime;
this.endTime = endTime;
this.description = description;
this.location = location;
this.week = week;
}
public String getEventTitle() {
return eventTitle;
}
public void setEventTitle(String eventTitle) {
this.eventTitle = eventTitle;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getWeek() {
return week;
}
public void setWeek(String week) {
this.week = week;
}
@Override
public String toString() {
return "CalenderDataStruct{" +
"eventTitle='" + eventTitle + '\'' +
", startTime='" + startTime + '\'' +
", endTime='" + endTime + '\'' +
", description='" + description + '\'' +
", location='" + location + '\'' +
", week='" + week + '\'' +
'}';
}
}
2、calendarUtil
package com.example.calendartest;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.telephony.TelephonyManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
public class calendarUtil {
private static String CALENDER_URL = "content://com.android.calendar/calendars";
private static String CALENDER_EVENT_URL = "content://com.android.calendar/events";
private static String CALENDER_REMINDER_URL = "content://com.android.calendar/reminders";
private static TelephonyManager mTm;
public static ArrayList<CalenderDataStruct> GetCalenderSchedule(Context context){
String startTime = "";
String endTime = "";
String eventTitle = "";
String description = "";
String location = "";
String week = "";
ArrayList<CalenderDataStruct> arr=new ArrayList<CalenderDataStruct>();
Cursor eventCursor = context.getContentResolver().query(Uri.parse(CALENDER_EVENT_URL), null,
null, null, "dtstart"+" DESC");
while (eventCursor.moveToNext()){
eventTitle = eventCursor.getString(eventCursor.getColumnIndex("title"));
description = eventCursor.getString(eventCursor.getColumnIndex("description"));
location = eventCursor.getString(eventCursor.getColumnIndex("eventLocation"));
startTime = timeStamp2Date(Long.parseLong(eventCursor.getString(eventCursor.getColumnIndex("dtstart"))));
endTime = timeStamp2Date(Long.parseLong(eventCursor.getString(eventCursor.getColumnIndex("dtend"))));
week = ""+ (getWeek(startTime));
CalenderDataStruct item=new CalenderDataStruct(eventTitle, startTime, endTime,description, location,week);
arr.add(item);
}
return arr;
}
private static JSONArray getcalendar(Context context){
String startTime = "";
String endTime = "";
String eventTitle = "";
String description = "";
String location = "";
JSONArray arr=new JSONArray();
Cursor eventCursor = context.getContentResolver().query(Uri.parse(CALENDER_EVENT_URL), null,
null, null, null);
while (eventCursor.moveToNext()){
JSONObject json=new JSONObject();
eventTitle = eventCursor.getString(eventCursor.getColumnIndex("title"));
description = eventCursor.getString(eventCursor.getColumnIndex("description"));
location = eventCursor.getString(eventCursor.getColumnIndex("eventLocation"));
startTime = timeStamp2Date(Long.parseLong(eventCursor.getString(eventCursor.getColumnIndex("dtstart"))));
endTime = timeStamp2Date(Long.parseLong(eventCursor.getString(eventCursor.getColumnIndex("dtend"))));
try {
json.put("eventTitle",eventTitle);
json.put("description",description);
json.put("location",location);
json.put("startTime",startTime);
json.put("endTime",endTime);
} catch (JSONException e) {
e.printStackTrace();
}
arr.put(json);
}
return arr;
}
/**
* 时间戳转换为字符串
* @param time:时间戳
* @return
*/
private static String timeStamp2Date(long time) {
String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date(time));
}
/**
* 判断当前日期是星期几
*
* @param pTime 设置的需要判断的时间 //格式如2012-09-08
*
* @return dayForWeek 判断结果
* @Exception 发生异常
*/
// String pTime = "2012-03-12";
private static int getWeek(String pTime) {
int Week = 0;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance();
try {
c.setTime(format.parse(pTime));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (c.get(Calendar.DAY_OF_WEEK) == 1) {
Week = 0;
}
if (c.get(Calendar.DAY_OF_WEEK) == 2) {
Week = 1;
}
if (c.get(Calendar.DAY_OF_WEEK) == 3) {
Week = 2;
}
if (c.get(Calendar.DAY_OF_WEEK) == 4) {
Week = 3;
}
if (c.get(Calendar.DAY_OF_WEEK) == 5) {
Week = 4;
}
if (c.get(Calendar.DAY_OF_WEEK) == 6) {
Week = 5;
}
if (c.get(Calendar.DAY_OF_WEEK) == 7) {
Week = 6;
}
return Week;
}
}
3、MainActivity
package com.example.calendartest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<CalenderDataStruct> calenderDataStructs = calendarUtil.GetCalenderSchedule(this);
Log.i(TAG, "onCreate: calenderDataStructs "+ calenderDataStructs.size());
for (CalenderDataStruct item : calenderDataStructs){
Log.i(TAG, "onCreate: CalenderDataStruct "+ item.toString());
}
}
}
4、AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.calendartest">
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
本文地址:https://blog.csdn.net/u014361280/article/details/108152733
上一篇: Linux详解 --- 进程管理
下一篇: 神经拟态技术会与 AI 芯片形成竞争吗?