Вопрос

hi everyone I am a newbie and learning android...i got the following code from this post written by @Abhi

Put reminder in real calendar on the phone?

this post does provide the answers, only code. I would like someone to help me with the problem I am having, first the code:

Code for onClickListenerEvent is below:

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 code:

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;
}

I was having issues with the code above, I found few answers in the post such as event1 is an URI type variable, but I am having the following issues:

Issues in onClickListenerEvent()

  1. The following code put red line underneath the code with this error

"The method getCalendarUriBase(Activity) in the type MainActivity is not applicable for the arguments (new View.OnClickListener(){})", the code is:

Uri.parse(getCalendarUriBase(this)+ "reminders");
  1. What is the return type of epoch, date and time in the following line?

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

  1. What is the following code? because in eclipse its says for alertDialog, 8 fixes available: 1st one is "create a new variable", if so what type of variable, what is dismiss? do I need them to get results or add events in the calendar?

    alertDialog.setTitle("Event Saved");

    Dismiss();

    alertDialog.show();

  2. On the following line in eclipse I get a deprecated warning:

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

Can @Abhi or any experienced person help me? As I am a newbie and android is not user fiendly at all.

Thanks.

Это было полезно?

Решение

Sounds like there are two things going on:

1) You have some basic questions about what the example code is doing or means. These are basic Java related questions

2) You're working with the calendar API (probably pre api 14?) and it's complicated

Regarding 1 with respect to "Issues in onClickListenerEvent()": you can't use "this" as a reference to the activity when invoking the getCalendarBaseUri() method since you're in an anonymous class at that point. Qualify the "this" keyword by preceding it with a class name. E.g., "MyActivity.this"

Regarding your second question, "getTime()" will return a long (c.f. http://developer.android.com/reference/java/util/Date.html).

Finally, the "managedQuery()" method on the Activity class was deprecated in API 11. That may or may not matter to you (depending on your project's goals).

All in all, it seems to me the calendar code is a little more complicated than it needs to be (probably because the original author was trying to support pushing events pre api 14?). Look up CalendarContract (http://developer.android.com/reference/android/provider/CalendarContract.html) if you have the luxury of targeting APIs 14+.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top