Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android设备上获取日历事件更新_Android - Fatal编程技术网

如何在android设备上获取日历事件更新

如何在android设备上获取日历事件更新,android,Android,我想在android 2.2设备上获取日历事件更新(当添加新事件或删除现有事件时)? 我是初学者,请告诉我此问题的逐步解决方案。此代码可能会帮助您:) 公共类myCalendar { 静态光标; 公共静态void readCalendar(上下文){ ContentResolver ContentResolver=context.getContentResolver(); //获取与设备同步的所有日历的列表、它们的显示名称以及 //用户已将其选中以显示。 cursor=contentResolv

我想在android 2.2设备上获取日历事件更新(当添加新事件或删除现有事件时)? 我是初学者,请告诉我此问题的逐步解决方案。

此代码可能会帮助您:)

公共类myCalendar
{
静态光标;
公共静态void readCalendar(上下文){
ContentResolver ContentResolver=context.getContentResolver();
//获取与设备同步的所有日历的列表、它们的显示名称以及
//用户已将其选中以显示。
cursor=contentResolver.query(Uri.parse(“content://com.android.calendar/calendars"),
(新字符串[]{“\u id”,“displayName”,“selected”}),null,null,null;
HashSet CalendarId=新HashSet();
尝试
{
System.out.println(“Count=“+cursor.getCount());
if(cursor.getCount()>0)
{
System.out.println(“控件就在cursor.count循环中”);
while(cursor.moveToNext()){
String _id=cursor.getString(0);
String displayName=cursor.getString(1);
所选布尔值=!cursor.getString(2).equals(“0”);
System.out.println(“Id:+\U Id+”显示名称:“+displayName+”选定:“+Selected”);
calendarID.add(_id);
}
}
}
捕获(断言错误示例)
{
例如printStackTrace();
}
捕获(例外e)
{
e、 printStackTrace();
}
//对于每个日历,显示从上周到下周结束的所有事件。
用于(字符串id:CalendarID){
Uri.Builder=Uri.parse(“content://com.android.calendar/instances/when)。buildon();
//Uri.Builder=Uri.parse(“content://com.android.calendar/calendars)。buildon();
long now=new Date().getTime();
ContentUris.appendId(生成器,现在为-DateUtils.DAY,单位为毫米*10000);
ContentUris.appendId(builder,now+DateUtils.DAY,单位为_MILLIS*10000);
Cursor eventCursor=contentResolver.query(builder.build(),
新字符串[]{“标题”、“开始”、“结束”、“全天”},日历。\u id=“+1,
空,“开始日期ASC,开始分钟ASC”);
System.out.println(“eventCursor count=“+eventCursor.getCount());
if(eventCursor.getCount()>0)
{
eventCursor.moveToFirst();
while(eventCursor.moveToNext())
{
对象请求日期、请求时间、结束日期、结束时间;
最终字符串标题=eventCursor.getString(0);
最终日期开始=新日期(eventCursor.getLong(1));
最终日期结束=新日期(eventCursor.getLong(2));
最终布尔值全天=!eventCursor.getString(3).equals(“0”);
/*System.out.println(“标题:“+Title+”开始:“+Begin+”结束:”+End+
“全天:”+全天);
*/  
System.out.println(“标题:”+标题);
System.out.println(“开始:+Begin”);
System.out.println(“结束:+End”);
System.out.println(“全天:+全天”);
System.out.println(“仅事件的日期开始=“+begin.getDate());
System.out.println(“事件的仅开始时间=“+begin.getHours()+”:“+begin.getMinutes()+”:“+begin.getSeconds());
System.out.println(“仅事件的开始日期=“+end.getDate());
System.out.println(“仅事件开始时间=“+end.getHours()+”:“+end.getMinutes()+”:“+end.getSeconds());
beg_date=begin.getDate();
beg_time=begin.getHours()+:“+begin.getMinutes();
end_date=end.getDate();
end_time=end.getHours()+:“+end.getMinutes();
}
}
打破
}
}
}

使用此代码,我可以知道哪个事件是从android设备更新和删除的。您可以对其进行进一步修改。目前它只从google日历获取事件:)这不支持android jelly bean。请给我指导行或内容uri名称。
public class myCalendar 
{

static Cursor cursor;

public static void readCalendar(Context context) {

    ContentResolver contentResolver = context.getContentResolver();

    // Fetch a list of all calendars synced with the device, their display names and whether the
    // user has them selected for display.


    cursor = contentResolver.query(Uri.parse("content://com.android.calendar/calendars"),
                (new String[] { "_id", "displayName", "selected"}), null, null, null);


    HashSet<String> calendarIds = new HashSet<String>();

    try
    {
        System.out.println("Count="+cursor.getCount());
        if(cursor.getCount() > 0)
        {
            System.out.println("the control is just inside of the cursor.count loop");
        while (cursor.moveToNext()) {

             String _id = cursor.getString(0);
             String displayName = cursor.getString(1);
             Boolean selected = !cursor.getString(2).equals("0");

            System.out.println("Id: " + _id + " Display Name: " + displayName + " Selected: " + selected);
            calendarIds.add(_id);
        }
    }
    }
    catch(AssertionError ex)
    {
        ex.printStackTrace();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }


    // For each calendar, display all the events from the previous week to the end of next week.        
    for (String id : calendarIds) {
        Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
        //Uri.Builder builder = Uri.parse("content://com.android.calendar/calendars").buildUpon();
        long now = new Date().getTime();

        ContentUris.appendId(builder, now - DateUtils.DAY_IN_MILLIS * 10000);
        ContentUris.appendId(builder, now + DateUtils.DAY_IN_MILLIS * 10000);

        Cursor eventCursor = contentResolver.query(builder.build(),
                new String[]  { "title", "begin", "end", "allDay"}, "Calendars._id=" + 1,
                null, "startDay ASC, startMinute ASC");

        System.out.println("eventCursor count="+eventCursor.getCount());
        if(eventCursor.getCount()>0)
        {

            eventCursor.moveToFirst();

            while (eventCursor.moveToNext())
            {
                Object beg_date,beg_time,end_date,end_time;  

                final String title = eventCursor.getString(0);
                final Date begin = new Date(eventCursor.getLong(1));
                final Date end = new Date(eventCursor.getLong(2));
                final Boolean allDay = !eventCursor.getString(3).equals("0");

        /*  System.out.println("Title: " + title + " Begin: " + begin + " End: " + end +
                    " All Day: " + allDay);
        */  
                System.out.println("Title:"+title);
                System.out.println("Begin:"+begin);
                System.out.println("End:"+end);
                System.out.println("All Day:"+allDay);

                System.out.println("only date begin of events="+begin.getDate());
                System.out.println("only begin time of events="+begin.getHours() + ":" +begin.getMinutes() + ":" +begin.getSeconds());

                System.out.println("only date begin of events="+end.getDate());
                System.out.println("only begin time of events="+end.getHours() + ":" +end.getMinutes() + ":" +end.getSeconds());

                beg_date = begin.getDate();
                beg_time = begin.getHours()+":"+begin.getMinutes();

                end_date = end.getDate();
                end_time = end.getHours()+":"+end.getMinutes();





            }
        }
        break;
    }
}


}