java - Android 日历事件问题

标签 java android android-calendar android-event

大家好,我是一名正在学习 android 的新手...我从 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. 以下代码在出现此错误的代码下方添加了红线

MainActivity 类型中的方法 getCalendarUriBase(Activity) 不适用于参数 (new View.OnClickListener(){})”,代码为:

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

  • 下面的代码是什么?因为在 Eclipse 中它表示 alertDialog,有 8 个可用修复:第一个是“创建一个新变量”,如果是的话,是什么类型的变量,什么是解雇?我需要它们来获取结果或在日历中添加事件吗?

    alertDialog.setTitle("事件已保存");

    解雇();

    alertDialog.show();

  • 在 Eclipse 中的以下行中,我收到一条已弃用的警告:

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

  • @Abhi 或任何有经验的人可以帮助我吗?因为我是一个新手,而且 android 根本不适合用户。

    谢谢。

    最佳答案

    听起来好像发生了两件事:

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

    2) 您正在使用日历 API(可能是 api 14 之前的版本?),它很复杂

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

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

    最后,API 11 中已弃用 Activity 类中的“managedQuery()”方法。这对您来说可能重要,也可能不重要(取决于您的项目目标)。

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

    关于java - Android 日历事件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16466583/

    相关文章:

    java - 无法执行目标 org.apache.maven.plugins :maven-compiler-plugin:3. 8.0:编译(默认编译)

    java - Java 中的匿名内部类(来自 Thinking in Java 第四版)

    java - if...elseif 一次又一次输入值时出错

    java - 尝试创建按钮数组,但应用程序在运行时崩溃并出现空指针异常

    java - 如何仅更改日历对象中的月,年和日期而不更改小时,分钟和秒?

    java - C2DM发送消息缓慢

    java - Android:在主菜单上显示高分

    android - 动态设置动画师 xml 值

    java - 发送日期参数到另一个Activity但值为零//android studio

    java - JAVA获取从设定日期起一周内所有日期的列表