日历事件在 Android 3.2 平板电脑中不起作用

发布于 2024-12-09 11:03:28 字数 6156 浏览 0 评论 0原文

我正在尝试在我的 android 平板电脑 3.2 中使用以下代码添加日历事件,

                                int[] calIds = null;
                                Cursor cursor = getContentResolver()
                                        .query(
                                                Uri
                                                        .parse("content://com.android.calendar/calendars"),
                                                new String[] { "_id",
                                                        "displayName" },
                                                "selected=1", null, null);
                                if (cursor != null && cursor.moveToFirst()) {
                                    String[] calNames = new String[cursor
                                            .getCount()];
                                    calIds = new int[cursor.getCount()];
                                    for (int i = 0; i < calNames.length; i++) {
                                        // retrieve the calendar names and
                                        // ids
                                        // at this stage you can print out
                                        // the display names to get an idea
                                        // of what calendars the user has
                                        calIds[i] = cursor.getInt(0);
                                        calNames[i] = cursor.getString(1);
                                        cursor.moveToNext();
                                    }
                                    cursor.close();
                                    if (calIds.length > 0) {
                                        // we're safe here to do any further
                                        // work
                                    }
                                }

                                // grab calendar id from above
                                int cal_id = calIds[1];

                                // set the content value
                                ContentValues cv = new ContentValues();

                                // make sure you add it to the right
                                // calendar
                                cv.put("calendar_id", cal_id);

                                // set the title of the event
                                cv.put("title", Name.getText().toString());

                                // set the description of the event
                                cv.put("description", Address.getText()
                                        .toString()
                                        + " ph:"
                                        + Phone.getText().toString());

                                // set the event's physical location
                                // cv.put("eventLocation", );

                                // set the start and end time
                                // note: you're going to need to convert the
                                // desired date into milliseconds

                                Calendar d = Calendar.getInstance();
                                d.set(2011, 10, 10, 3, 0, 0);
                                Calendar end = Calendar.getInstance();
                                end.set(2011, 10, 10, 9, 0, 0);

                                long startTime = d.getTimeInMillis();
                                long endTime = end.getTimeInMillis();
                                endTime = endTime * 1000;

                                startTime = startTime * 1000;
                                System.out.println("HOW TO SOL:VCE THIS");

                                System.out.println(startTime);
                                System.out.println(endTime);

                                cv.put("dtstart", startTime);

                                cv.put("dtend", endTime);



                                cv.put("rrule",
                                        "FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR"); // let
                                                                            // the
                                                                            // calendar
                                                                            // know
                                                                            // whether
                                                                            // this
                                                                            // event
                                                                            // goes
                                                                            // on
                                                                            // all
                                                                            // day
                                                                            // or
                                                                            // not
                                // true = 1, false = 0

                                // let the calendar know whether an alarm
                                // should go off for this event

                                // once desired fields are set, insert it
                                // into the table
                                getContentResolver()
                                        .insert(
                                                Uri
                                                        .parse("content://com.android.calendar/events"),
                                                cv);

但我无法插入该事件。

在日志中我得到以下内容

10-11 12:51:47.250: INFO/CalendarProvider2(24267): Sending notification intent: Intent { act=android.intent.action.PROVIDER_CHANGED dat=content://com.android.calendar }
10-11 12:51:47.250: WARN/ContentResolver(24267): Failed to get type for: content://com.android.calendar (Unknown URL content://com.android.calendar)

我应该如何解决这个问题任何帮助.. 提前致谢..

I am trying to add calendar event using the below code in my android tablet 3.2

                                int[] calIds = null;
                                Cursor cursor = getContentResolver()
                                        .query(
                                                Uri
                                                        .parse("content://com.android.calendar/calendars"),
                                                new String[] { "_id",
                                                        "displayName" },
                                                "selected=1", null, null);
                                if (cursor != null && cursor.moveToFirst()) {
                                    String[] calNames = new String[cursor
                                            .getCount()];
                                    calIds = new int[cursor.getCount()];
                                    for (int i = 0; i < calNames.length; i++) {
                                        // retrieve the calendar names and
                                        // ids
                                        // at this stage you can print out
                                        // the display names to get an idea
                                        // of what calendars the user has
                                        calIds[i] = cursor.getInt(0);
                                        calNames[i] = cursor.getString(1);
                                        cursor.moveToNext();
                                    }
                                    cursor.close();
                                    if (calIds.length > 0) {
                                        // we're safe here to do any further
                                        // work
                                    }
                                }

                                // grab calendar id from above
                                int cal_id = calIds[1];

                                // set the content value
                                ContentValues cv = new ContentValues();

                                // make sure you add it to the right
                                // calendar
                                cv.put("calendar_id", cal_id);

                                // set the title of the event
                                cv.put("title", Name.getText().toString());

                                // set the description of the event
                                cv.put("description", Address.getText()
                                        .toString()
                                        + " ph:"
                                        + Phone.getText().toString());

                                // set the event's physical location
                                // cv.put("eventLocation", );

                                // set the start and end time
                                // note: you're going to need to convert the
                                // desired date into milliseconds

                                Calendar d = Calendar.getInstance();
                                d.set(2011, 10, 10, 3, 0, 0);
                                Calendar end = Calendar.getInstance();
                                end.set(2011, 10, 10, 9, 0, 0);

                                long startTime = d.getTimeInMillis();
                                long endTime = end.getTimeInMillis();
                                endTime = endTime * 1000;

                                startTime = startTime * 1000;
                                System.out.println("HOW TO SOL:VCE THIS");

                                System.out.println(startTime);
                                System.out.println(endTime);

                                cv.put("dtstart", startTime);

                                cv.put("dtend", endTime);



                                cv.put("rrule",
                                        "FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR"); // let
                                                                            // the
                                                                            // calendar
                                                                            // know
                                                                            // whether
                                                                            // this
                                                                            // event
                                                                            // goes
                                                                            // on
                                                                            // all
                                                                            // day
                                                                            // or
                                                                            // not
                                // true = 1, false = 0

                                // let the calendar know whether an alarm
                                // should go off for this event

                                // once desired fields are set, insert it
                                // into the table
                                getContentResolver()
                                        .insert(
                                                Uri
                                                        .parse("content://com.android.calendar/events"),
                                                cv);

But i am unable to insert the event.

in log i am getting the following

10-11 12:51:47.250: INFO/CalendarProvider2(24267): Sending notification intent: Intent { act=android.intent.action.PROVIDER_CHANGED dat=content://com.android.calendar }
10-11 12:51:47.250: WARN/ContentResolver(24267): Failed to get type for: content://com.android.calendar (Unknown URL content://com.android.calendar)

How should i solve this any help..
Thanks in advance..

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

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

发布评论

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

评论(1

仙女山的月亮 2024-12-16 11:03:28

在这里我可以将事件插入日历中。

    final ContentResolver cr = getContentResolver();
    Cursor cursor ;


    //Retrieving calendar sync data 


    if (Integer.parseInt(Build.VERSION.SDK) == 8 )
        cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
    else
        cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
    if(cursor.moveToFirst()) 
    {
        final String[] calNames = new String[cursor.getCount()];
        final int[] calIds = new int[cursor.getCount()];
        for (int i = 0; i < calNames.length; i++) 
        {
            calIds[i] = cursor.getInt(0);
            calNames[i] = cursor.getString(1);
            System.out.println("Title: " +calIds[i] + " name " + calNames[i]);
            cursor.moveToNext();
        }
        System.out.println("currenttimeinmillis"+System.currentTimeMillis());



        AlertDialog.Builder builder = new AlertDialog.Builder(calendarsync.this);
        builder.setSingleChoiceItems(calNames, -1, new DialogInterface.OnClickListener() 
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {

                **

// 在日历中插入/同步事件

**

                ContentValues cv = new ContentValues();
                cv.put("calendar_id", calIds[which]);
                cv.put("title", "Naven");
                // set the description of the event
                cv.put("description", "Birthday");
                // set the event's physical location
                // cv.put("eventLocation", );

                Calendar d = Calendar.getInstance();
                d.set(2012, 0, 10, 3, 0, 0);
                Calendar end = Calendar.getInstance();
                end.set(2012, 0, 10, 4, 0, 0);

                long startTime = d.getTimeInMillis();
                long endTime = end.getTimeInMillis();
               // endTime = endTime * 1000;

               // startTime = startTime * 1000;
                System.out.println("HOW TO SOL:VCE THIS");

                System.out.println(startTime);
                System.out.println(endTime);

                cv.put("dtstart", startTime);

                cv.put("dtend", endTime);
                cv.put("rrule","FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR"); 
                // let the calendar know whether this
                // event goes on all day or not
                // true = 1, false = 0

                cv.put("hasAlarm", 1);

                Uri newEvent ;
                if (Integer.parseInt(Build.VERSION.SDK) == 8 )
                    newEvent = cr.insert(Uri.parse("content://com.android.calendar/events"), cv);
                else
                    newEvent = cr.insert(Uri.parse("content://com.android.calendar/events"), cv);

                if (newEvent != null) 
                {
                    long id = Long.parseLong( newEvent.getLastPathSegment() );
                    ContentValues values = new ContentValues();
                    values.put( "event_id", id );
                    values.put( "method", 1 );
                    values.put( "minutes", 15 ); // 15 minuti
                    if (Integer.parseInt(Build.VERSION.SDK) == 8 )
                        cr.insert( Uri.parse( "content://com.android.calendar/reminders" ), values );
                    else
                        cr.insert( Uri.parse( "content://calendar/reminders" ), values );

                }
                dialog.cancel();
            }

        });

        builder.create().show();

    }    
    cursor.close();

here I am able to insert events in to calendar.

    final ContentResolver cr = getContentResolver();
    Cursor cursor ;


    //Retrieving calendar sync data 


    if (Integer.parseInt(Build.VERSION.SDK) == 8 )
        cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
    else
        cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
    if(cursor.moveToFirst()) 
    {
        final String[] calNames = new String[cursor.getCount()];
        final int[] calIds = new int[cursor.getCount()];
        for (int i = 0; i < calNames.length; i++) 
        {
            calIds[i] = cursor.getInt(0);
            calNames[i] = cursor.getString(1);
            System.out.println("Title: " +calIds[i] + " name " + calNames[i]);
            cursor.moveToNext();
        }
        System.out.println("currenttimeinmillis"+System.currentTimeMillis());



        AlertDialog.Builder builder = new AlertDialog.Builder(calendarsync.this);
        builder.setSingleChoiceItems(calNames, -1, new DialogInterface.OnClickListener() 
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {

                **

// Inserting/syncing events in calendar

**

                ContentValues cv = new ContentValues();
                cv.put("calendar_id", calIds[which]);
                cv.put("title", "Naven");
                // set the description of the event
                cv.put("description", "Birthday");
                // set the event's physical location
                // cv.put("eventLocation", );

                Calendar d = Calendar.getInstance();
                d.set(2012, 0, 10, 3, 0, 0);
                Calendar end = Calendar.getInstance();
                end.set(2012, 0, 10, 4, 0, 0);

                long startTime = d.getTimeInMillis();
                long endTime = end.getTimeInMillis();
               // endTime = endTime * 1000;

               // startTime = startTime * 1000;
                System.out.println("HOW TO SOL:VCE THIS");

                System.out.println(startTime);
                System.out.println(endTime);

                cv.put("dtstart", startTime);

                cv.put("dtend", endTime);
                cv.put("rrule","FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR"); 
                // let the calendar know whether this
                // event goes on all day or not
                // true = 1, false = 0

                cv.put("hasAlarm", 1);

                Uri newEvent ;
                if (Integer.parseInt(Build.VERSION.SDK) == 8 )
                    newEvent = cr.insert(Uri.parse("content://com.android.calendar/events"), cv);
                else
                    newEvent = cr.insert(Uri.parse("content://com.android.calendar/events"), cv);

                if (newEvent != null) 
                {
                    long id = Long.parseLong( newEvent.getLastPathSegment() );
                    ContentValues values = new ContentValues();
                    values.put( "event_id", id );
                    values.put( "method", 1 );
                    values.put( "minutes", 15 ); // 15 minuti
                    if (Integer.parseInt(Build.VERSION.SDK) == 8 )
                        cr.insert( Uri.parse( "content://com.android.calendar/reminders" ), values );
                    else
                        cr.insert( Uri.parse( "content://calendar/reminders" ), values );

                }
                dialog.cancel();
            }

        });

        builder.create().show();

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