Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.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 是否有办法访问日历';在不使用GDataJava客户机的情况下删除条目?_Android_Android Calendar - Fatal编程技术网

Android 是否有办法访问日历';在不使用GDataJava客户机的情况下删除条目?

Android 是否有办法访问日历';在不使用GDataJava客户机的情况下删除条目?,android,android-calendar,Android,Android Calendar,是否可以从手机脱机获取日历条目?似乎唯一的方法就是使用。您可以使用日历内容提供程序(com.android.providers.calendar.CalendarProvider)。例如: ContentResolver contentResolver = context.getContentResolver(); Cursor cursor = contentResolver.query(Uri.parse("content://calendar/events"), null, null,

是否可以从手机脱机获取日历条目?似乎唯一的方法就是使用。

您可以使用日历内容提供程序(
com.android.providers.calendar.CalendarProvider
)。例如:


ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(Uri.parse("content://calendar/events"), null, null, null, null);

while(cursor.moveToNext()) {
    String eventTitle = cursor.getString(cursor.getColumnIndex("title"));
    Date eventStart = new Date(cursor.getLong(cursor.getColumnIndex("dtstart")));
    // etc.
}


编辑:您可能希望将其放入包装器中(请参阅),因为它当前是一个私有API。

当前,如果不使用私有API,这是不可能的(请参阅Josef的帖子)。有一个日历提供程序,但它尚未公开。它可能随时更改并破坏您的应用程序。
不过,它可能不会改变(我不认为他们会从“日历”更改它),所以您可能可以使用它。但我的建议是使用这样一个单独的类:

public class CalendarProvider {
     public static final Uri CONTENT_URI = Uri.parse("content://calendar");
     public static final String TITLE = "title";
     public static final String ....
public class CalendarEvent {
    private long id;
    private long date;
    //etc...
}

public interface CalendarService {

    public Set<CalendarEvent> getAllCalendarEvents();

    public CalendarEvent findCalendarEventById(long id);

    public CalendarEvent findCalendarEventByDate(long date);

}
Class<?> calendarProviderClass = Class.forName("android.provider.Calendar");
Field uriField = calendarProviderClass.getField("CONTENT_URI");
Uri calendarUri = (Uri) uriField.get(null);

并直接使用这些而不是字符串。这将使您在API更改或公开时可以非常轻松地更改它。

关于可以更改的API。。。整个ContentProvider方法不会很快改变,因此只需更新字符串就可以克服很多问题。因此,创建在整个项目中重用的常量

public static final String URI_CONTENT_CALENDAR_EVENTS = "content://calendar/events";

ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(Uri.parse(URI_CONTENT_CALENDAR_EVENTS), null, null, null, null);
    //etc
如果您想要一个合适的私有API,您必须创建一个pojo和一些如下服务:

public class CalendarProvider {
     public static final Uri CONTENT_URI = Uri.parse("content://calendar");
     public static final String TITLE = "title";
     public static final String ....
public class CalendarEvent {
    private long id;
    private long date;
    //etc...
}

public interface CalendarService {

    public Set<CalendarEvent> getAllCalendarEvents();

    public CalendarEvent findCalendarEventById(long id);

    public CalendarEvent findCalendarEventByDate(long date);

}
Class<?> calendarProviderClass = Class.forName("android.provider.Calendar");
Field uriField = calendarProviderClass.getField("CONTENT_URI");
Uri calendarUri = (Uri) uriField.get(null);
公共类日历事件{
私人长id;
私人长约会;
//等等。。。
}
公共接口日历服务{
公共设置getAllCalendarEvents();
公共日历事件findCalendarEventById(长id);
公共日历事件findCalendarEventByDate(长日期);
}

等等。这样,您只需在API发生变化时更新CalendarEvent对象和此服务。

约瑟夫和艾萨克的日历访问解决方案仅适用于Android 2.1及更早版本。谷歌已将2.2中的基本内容URI从content://calendar“到”content://com.android.calendar". 此更改意味着最好的方法是尝试使用旧的基本URI获取游标,如果返回的游标为null,则尝试使用新的基本URI

请注意,我从Shane Conder和Lauren Darcey提供的文章中得到了这种方法


这些答案很好,但它们都涉及到对日历URI进行硬编码(我在不同Android设备的三个不同版本中看到了这一点)

获取
URI
(硬编码类和字段的名称)的更好方法如下:

public class CalendarProvider {
     public static final Uri CONTENT_URI = Uri.parse("content://calendar");
     public static final String TITLE = "title";
     public static final String ....
public class CalendarEvent {
    private long id;
    private long date;
    //etc...
}

public interface CalendarService {

    public Set<CalendarEvent> getAllCalendarEvents();

    public CalendarEvent findCalendarEventById(long id);

    public CalendarEvent findCalendarEventByDate(long date);

}
Class<?> calendarProviderClass = Class.forName("android.provider.Calendar");
Field uriField = calendarProviderClass.getField("CONTENT_URI");
Uri calendarUri = (Uri) uriField.get(null);
Class calendarProviderClass=Class.forName(“android.provider.Calendar”);
Field uriField=calendarProviderClass.getField(“内容URI”);
Uri calendarUri=(Uri)uriField.get(null);
这并不完美(如果他们删除了
android.provider.Calendar
类或
CONTENT\u URI
字段,它就会崩溃),但它比任何单一URI硬代码在更多平台上工作


请注意,这些反射方法将抛出异常,调用方法需要捕获或重新抛出异常。

Nick的解决方案涉及managedQuery,它未在上下文类中定义。很多时候,在后台运行时,您可能希望使用上下文对象。以下是一个修改版本:

公共字符串getCalendarUriBase(){

返回(android.os.Build.VERSION.SDK\u INT>=8)? "content://com.android.calendar": "content://calendar"; }


此处不应执行null的捕获,因为即使managedQuery先前成功,也可能会出现更多异常

您可以在此处使用日历合同:


它与Android 4上可用的API类相同,但与Android>=2.2一起使用。

是的,但这是一个随时可能中断的私有API。你完全正确,你的包装器显然是一个非常好的主意,即使当/如果他们更改某些内容时,你仍然必须返回到应用程序的代码中。一旦API公开,包装器可能就过时了。谢谢!顺便说一句,你能用最新的SDK(1.5 R1)使用日历(通过从源代码中构建2 apk)和DevTools中的“谷歌登录服务”吗?谷歌登录服务是谷歌专有的东西,除非你有真正的手机,否则它总是崩溃。我还没有试过日历。请注意,从API 14开始,它就不再工作了。有一个官方的日历api(14+),它涉及到使用新的“类”