2013-05-09 47 views
0

大家好我是一个新手,学习Android的...我得到了来自this post写了下面的代码通过@AbhiAndroid的日历事件发出

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类型变量,但我有以下问题:

onC中的问题lickListenerEvent()

  1. 下面的代码把红线的代码下方与此错误

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

Uri.parse(getCalendarUriBase(this)+ "reminders"); 
  1. 什么是下一行的历元,日期和时间的返回类型?

epoch = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm a").parse(date+" "+time).getTime();

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

    alertDialog.setTitle("Event Saved");

    Dismiss();

    alertDialog.show();

  • 在Eclipse中以下行我得到一个过时的警告:

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

  • @Abhi或任何有经验的人都可以帮助我吗?由于我是新手,并且Android根本不是用户。

    谢谢。

    回答

    2

    听起来像有两件事情正在进行:

    1)您有什么示例代码是做或手段的一些基本问题。这些都是基本的Java相关的问题

    2)你与日历API(可能是前API 14)的工作,它的复杂

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

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

    最后,在活动类“managedQuery()”方法在API 11可能会或可能不算什么,你已被否决(取决于你的项目的目标)。

    一切的一切,在我看来,日历代码是有点复杂得多,它需要的(可能是因为原作者正试图以支持推动事件前API 14?)。查看CalendarContract(http://developer.android.com/reference/android/provider/CalendarContract.html),如果您有奢侈的定位API 14+。