在 2.0 及以上操作系统中添加日历事件时出现错误

发布于 2024-10-14 09:53:20 字数 1974 浏览 2 评论 0原文

我正在使用下面的代码。它在 android 1.6 上工作正常,但在 android 2.0 及更高版本上抛出以下错误。请让我知道它的解决方案。

错误:

01-24 16:55:28.315:错误/ActivityThread(208):无法找到日历的提供程序信息 01-24 16:55:28.315: ERROR/error(208): 未知 URL content://calendar/events

要阅读活动:

private void readContent(String uriString) {

        Uri uri = Uri.parse(uriString);
        Cursor cursor = getContentResolver().query(uri, null, null,
                null, null);
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            String columnNames[] = cursor.getColumnNames();
            String value = "";
            String colNamesString = "";
            do {
                value = "";

                for (String colName : columnNames) {
                    value += colName + " = ";
                    value += cursor.getString(cursor.getColumnIndex(colName))
                    + " ||";
                }

                Log.e("INFO : ", value);
            } while (cursor.moveToNext());

        }

    }

要添加活动

private void addEvent(){
    try {
        ContentValues event = new ContentValues();
        event.put("calendar_id", "1");
        event.put("title", "tet event");
        event.put("description", "hello this is testing of event");
        event.put("eventLocation", "Ahmedabad");
        Calendar c = Calendar.getInstance();
        long date = c.getTimeInMillis();
        event.put("dtstart", date);
        event.put("dtend", date);
        event.put("allDay", 1);
        event.put("eventStatus", 1);
        event.put("hasAlarm", 1);
        Uri eventsUri = Uri.parse("content://calendar/events");
        Uri url = getContentResolver().insert(eventsUri, event);
        Log.e("uri", url.toString());
    } catch (Exception e) {
        Log.e("error", e.getMessage());
        e.printStackTrace();
    }
}

谢谢

I am using below code. Its working fine for android 1.6 but its throwing below error for android 2.0 and above version. Please let me know the solution for it.

Error:

01-24 16:55:28.315: ERROR/ActivityThread(208): Failed to find provider info for calendar
01-24 16:55:28.315: ERROR/error(208): Unknown URL content://calendar/events

To read Event:

private void readContent(String uriString) {

        Uri uri = Uri.parse(uriString);
        Cursor cursor = getContentResolver().query(uri, null, null,
                null, null);
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            String columnNames[] = cursor.getColumnNames();
            String value = "";
            String colNamesString = "";
            do {
                value = "";

                for (String colName : columnNames) {
                    value += colName + " = ";
                    value += cursor.getString(cursor.getColumnIndex(colName))
                    + " ||";
                }

                Log.e("INFO : ", value);
            } while (cursor.moveToNext());

        }

    }

To Add event

private void addEvent(){
    try {
        ContentValues event = new ContentValues();
        event.put("calendar_id", "1");
        event.put("title", "tet event");
        event.put("description", "hello this is testing of event");
        event.put("eventLocation", "Ahmedabad");
        Calendar c = Calendar.getInstance();
        long date = c.getTimeInMillis();
        event.put("dtstart", date);
        event.put("dtend", date);
        event.put("allDay", 1);
        event.put("eventStatus", 1);
        event.put("hasAlarm", 1);
        Uri eventsUri = Uri.parse("content://calendar/events");
        Uri url = getContentResolver().insert(eventsUri, event);
        Log.e("uri", url.toString());
    } catch (Exception e) {
        Log.e("error", e.getMessage());
        e.printStackTrace();
    }
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

君勿笑 2024-10-21 09:53:20

您应该知道,在新的 Android 版本上,日历内容提供程序的 URI 已更改,现在您应该使用 content://com.android.calendar/

是的,这是废话:(

所以 如果您之前使用 content://calendar/ ,为了获得成功,现在您应该使用 content://com.android.calendar/

如果您想保持应用程序的所有 Android 版本之间的兼容性,您将需要处理旧 URI 和新 URI,您可以执行以下操作:

Uri calendarUri;
Uri eventUri;
if (android.os.Build.VERSION.SDK_INT <= 7 )
{
  //the old way
  calendarUri = Uri.parse("content://calendar/calendars"); 
  eventUri    = Uri.parse("content://calendar/events");
}
else
{
 //the new way
 calendarUri = Uri.parse("content://com.android.calendar/calendars");
 eventUri    = Uri.parse("content://com.android.calendar/events");
} 

但是,让我们玩一点 xDDD

function Uri getCalendarURI(eventUri boolean){
Uri calendarURI = null;

    if (android.os.Build.VERSION.SDK_INT <= 7 )
    {
         calendarURI = (eventUri)?Uri.parse("content://calendar/events"):Uri.parse("content://calendar/calendars");
    }
    else
        {
        calendarURI = (eventUri)?Uri.parse("content://com.android.calendar/events"): Uri.parse("content://com.android.calendar/calendars");
    } 
 return calendarURI; 
}

或者在一行中:

function Uri getCalendarUri(eventUri boolean){
    return  (android.os.Build.VERSION.SDK_INT <= 7 )?((eventUri)?Uri.parse("content://calendar/events"):Uri.parse("content://calendar/calendars")):(calendarURI = (eventUri)?Uri.parse("content://com.android.calendar/events"): Uri.parse("content://com.android.calendar/calendars"));
}

注意:android.os.Build.VERSION.SDK_INT 可用,因为 SDK_INT = 4 我的意思是Android 1.6,对于先前版本 android.os.Build.VERSION.SDK 更多信息位于 http ://developer.android.com/reference/android/os/Build.VERSION

You should know, that on new Android versions the URI for Calendar content provider has changed, now you should use content://com.android.calendar/

Yes it´s a crap :(

So if you was using content://calendar/ ,to get successful,now you should use content://com.android.calendar/

If you want to maintain a compatibility across all Android versions of your apps, you will need to handle the Old URI along with the New URI, you can do something like this:

Uri calendarUri;
Uri eventUri;
if (android.os.Build.VERSION.SDK_INT <= 7 )
{
  //the old way
  calendarUri = Uri.parse("content://calendar/calendars"); 
  eventUri    = Uri.parse("content://calendar/events");
}
else
{
 //the new way
 calendarUri = Uri.parse("content://com.android.calendar/calendars");
 eventUri    = Uri.parse("content://com.android.calendar/events");
} 

But, lets play a bit xDDD

function Uri getCalendarURI(eventUri boolean){
Uri calendarURI = null;

    if (android.os.Build.VERSION.SDK_INT <= 7 )
    {
         calendarURI = (eventUri)?Uri.parse("content://calendar/events"):Uri.parse("content://calendar/calendars");
    }
    else
        {
        calendarURI = (eventUri)?Uri.parse("content://com.android.calendar/events"): Uri.parse("content://com.android.calendar/calendars");
    } 
 return calendarURI; 
}

Or in one line :

function Uri getCalendarUri(eventUri boolean){
    return  (android.os.Build.VERSION.SDK_INT <= 7 )?((eventUri)?Uri.parse("content://calendar/events"):Uri.parse("content://calendar/calendars")):(calendarURI = (eventUri)?Uri.parse("content://com.android.calendar/events"): Uri.parse("content://com.android.calendar/calendars"));
}

Note : android.os.Build.VERSION.SDK_INT is available since SDK_INT = 4 i mean Android 1.6,for prior version android.os.Build.VERSION.SDK more info at http://developer.android.com/reference/android/os/Build.VERSION

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文