android日历事件问题

时间:2013-05-09 16:29:01

标签: java android android-calendar android-event

大家好,我是新手,学习安卓...我从this post撰写的@Abhi获得了以下代码

Put reminder in real calendar on the phone?

这篇文章确实提供了答案,只提供了代码。我希望有人帮我解决我遇到的问题,首先是代码:

onClickListenerEvent的代码如下:

btnOne.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // Calendar dateToShow = Calendar.getInstance();
                // dateToShow.set(2013, Calendar.MAY, 10, 9, 0);
                //
                // showCalendarAtTime(dateToShow);
                Uri event1;
                long epoch, epoch1;
                Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
                ContentResolver cr = getContentResolver();

                ContentValues values = new ContentValues();

                try 
                {
                    epoch = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm a").parse(date+" "+time).getTime();
                    //epoch=epoch;
                    Log.e("epoch",String.valueOf(epoch));
                    epoch1 = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm a").parse(date+" "+time).getTime();
                    //epoch1=epoch1;
                    Log.e("epoch1",String.valueOf(epoch1));
                } catch (ParseException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                values.put("calendar_id", 1);
                values.put("title", "Appoitment");
                values.put("allDay", 0);
                values.put("dtstart",epoch); // event starts at 11 minutes from now
                values.put("dtend", epoch1 ); // ends 60 minutes from now
                values.put("description", "Your consulting date and time ");
                values.put("visibility", 0);
                values.put("hasAlarm", 1);
                if(EVENTS_URI!=null){
                    event1 = cr.insert(EVENTS_URI, values);
                }

                // reminder insert
                Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
                values = new ContentValues();
                values.put( "event_id", Long.parseLong(event1.getLastPathSegment()));
                values.put( "method", 1 );
                values.put( "minutes", 10 );
                if(REMINDERS_URI!=null){   
                    cr.insert( REMINDERS_URI, values );
                }
                alertDialog.setTitle("Event Saved");
                Dismiss();
                alertDialog.show();
                }

                // reminder insert
                Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this)+ "reminders");
                values = new ContentValues();
                values.put("event_id", id);
                values.put("method", 1);
                values.put("minutes", 0);
                cr.insert(REMINDERS_URI, values);

            }

        });

getCalendarUriBase代码:

private String getCalendarUriBase(Activity act) {
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try {
    managedCursor = act.managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
    calendarUriBase = "content://calendar/";
} else {
    calendars = Uri.parse("content://com.android.calendar/calendars");
    try {
        managedCursor = act.managedQuery(calendars, null, null, null, null);
    } catch (Exception e) {
    }
    if (managedCursor != null) {
        calendarUriBase = "content://com.android.calendar/";
    }
}
return calendarUriBase;
}

我遇到上述代码的问题,我在帖子中找到了一些答案,例如event1是一个URI类型变量,但我遇到了以下问题:

onClickListenerEvent()

中的问题
  1. 以下代码在代码下方放置了红线
  2. "类型getCalendarUriBase(Activity)中的方法MainActivity不适用于参数(new View.OnClickListener(){})",代码为:

    Uri.parse(getCalendarUriBase(this)+ "reminders");
    
    1. 以下行中的纪元,日期和时间的返回类型是什么?
    2. epoch = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm a").parse(date+" "+time).getTime();

      1. 以下代码是什么?因为在eclipse中它代表alertDialog,有8个修复可用:第一个是"创建一个新变量",如果是这样,什么类型的变量,什么是解雇?我是否需要它们来获取结果或在日历中添加事件?

        alertDialog.setTitle("Event Saved");

        Dismiss();

        alertDialog.show();

      2. 在eclipse的以下行中,我收到了一个弃用的警告:

        managedCursor = act.managedQuery(calendars, null, null, null, null);

      3. @Abhi或任何有经验的人可以帮助我吗?由于我是新手,而且Android根本不是用户友情。

        感谢。

1 个答案:

答案 0 :(得分:2)

听起来有两件事情发生了:

1)您对示例代码的作用或方法有一些基本问题。这些是与Java相关的基本问题

2)你正在使用日历API(可能是pre api 14?)而且它很复杂

关于“onClickListenerEvent()中的问题”关于1:在调用getCalendarBaseUri()方法时,不能使用“this”作为对活动的引用,因为此时您处于匿名类中。通过在其前面加上类名来限定“this”关键字。例如,“MyActivity.this”

关于你的第二个问题,“getTime()”将返回一个长整数(c.f。http://developer.android.com/reference/java/util/Date.html)。

最后,在Activity 11中不推荐使用Activity类上的“managedQuery()”方法。这可能对您有用,也可能无关紧要(取决于您项目的目标)。

总而言之,在我看来,日历代码比它需要的要复杂得多(可能是因为原作者试图支持在api 14之前推送事件?)。如果你有足够的目标API 14 +,请查看CalendarContract(http://developer.android.com/reference/android/provider/CalendarContract.html)。