2011-12-08 13 views
0

Androidカレンダーから予定を作成する必要があります。私はカレンダーAPIがあると信じていますが、私は決してそれを使用していません。私はAndroid開発にかなり新しいですので、ブラウジングからいくつかの例を見つけて、次のコードを使用してAndroidカレンダーを更新しようとしました。Androidコードからカレンダーを動的に更新するには

public static boolean updateCalendar(Context context,String cal_Id,String eventId) 
{ 
try{ 

    Uri CALENDAR_URI = Uri.parse(CAL_URI+"events"); 
    Cursor c = context.getContentResolver().query(CALENDAR_URI, null, null, null, null); 
    String[] s = c.getColumnNames(); 

    if (c.moveToFirst()) 
    { 
      while (c.moveToNext()) 
     { 

      String _id = c.getString(c.getColumnIndex("_id")); 
      String CalId = c.getString(c.getColumnIndex("calendar_id"));    
      if ((_id==null) && (CalId == null)) 
      { 
          return false; 
      } 
      else 
      { 
       if (_id.equals(eventId) && CalId.equals(cal_Id)) 
          { 
        Uri uri = ContentUris.withAppendedId(CALENDAR_URI, Integer.parseInt(_id)); 
        context.getContentResolver().update(uri, null, null, null);// need to give your data here 
        return true; 
       } 
      } 
     } 
    } 


} 
finally 
{ 
    return true; 
} 
} 

しかし、私はそれを呼び出されませんgetColumnNamesを実行し、コードが直線context.getContentResolver()更新(URI、NULL、NULL、NULL)にジャンプするとき。終了します。

カレンダーにいくつかのテストイベントを入れましたが、なぜコードがそれらを取り上げていないのですか?

答えて

0

使用この特定のカレンダにイベントを追加するには、日付と時刻

Uri event1; 
    long epoch; 
    long 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").parse(YourStartDate+" "+YourStratTime).getTime(); 
        //epoch=epoch; 
        Log.e("epoch",String.valueOf(epoch)); 
        epoch1 = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm").parse(YourStartDate+" "+YourEndDate).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); 
       } 

getCalendarUroBase機能:

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

注:サンプル日ごとのようYYYY-MM-DDにする必要があり、時間べきhh:mm形式にする

+0

私のアプリケーションにコードを埋め込んでいますが、いくつかのエラーメッセージが表示されています。 2行のコードUri EVENTS_URI = Uri.parse(getCalendarUriBase(this)+ "events"); ContentResolver cr = getContentResolver();最初に「静的コンテキストでは使用できません」と表示され、2番目に「非静的メソッドgetContentResolver()への静的参照をContextWrapperタイプから作成できません」というエラーメッセージが表示されます。 – user616076

+0

静的メソッドでコードを配置したことがありますか?一度チェックしてください。エミュレータでは機能しない可能性があります – Abhi

+0

http://stackoverflow.com/questions/4969171/cannot-make-a-static-reference-to-the-non-static-メソッドはこれをチェックします – Abhi