2016-10-04 2 views
-1

私はアンドロイドでカレンダーにイベントを挿入するには、このコードを使用しています:ここで挿入イベント

private void insertEvent(int start, int end, String location, String comment, String title) { 
    ContentValues values = new ContentValues(); 
    TimeZone tz = TimeZone.getDefault(); 

    values.put("calendar_id", 1); 
    values.put("title", title); 
    values.put("description", comment); 
    values.put("eventLocation", location); 
    values.put("dtstart", start); 
    values.put("dtend", end); 
    values.put("allDay", 0); 
    values.put("rrule", "FREQ=YEARLY"); 
    values.put("eventTimezone", tz.getID()); 

    Uri l_eventUri; 
    if (android.os.Build.VERSION.SDK_INT <= 7) { 
     // the old way 
     l_eventUri = Uri.parse("content://calendar/events"); 
    } else { 
     // the new way 
     l_eventUri = Uri.parse("content://com.android.calendar/events"); 
    } 
    Uri l_uri = getActivity().getContentResolver() 
      .insert(l_eventUri, values); 
} 

は別の試みです:

private void insertEvent2(int start, int end, String location, String comment, String title){ 

    ContentResolver cr = getActivity().getContentResolver(); 
    ContentValues eventsArray = new ContentValues(); 
     ContentValues values = new ContentValues(); 
     TimeZone timeZone = TimeZone.getDefault(); 
     values.put(CalendarContract.Events.DTSTART,start); 
     values.put(CalendarContract.Events.DTEND,end); 
     values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID()); 
     values.put(CalendarContract.Events.TITLE, title); 
     values.put(CalendarContract.Events.DESCRIPTION, title); 
     values.put(CalendarContract.Events.EVENT_LOCATION,location); 
     values.put(CalendarContract.Events.CALENDAR_ID, 1); 
     if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return; 
     } 
     eventsArray = values; 
    Uri l_uri = getActivity().getContentResolver() 
      .insert(CalendarContract.Events.CONTENT_URI, values); 
} 

しかし、私はイベントを見ることができませんデフォルトのアンドロイドカレンダーが表示されたら作成しました。どのようにイベントを挿入すべきですか?

+0

は、これら2つのリンクを参照してくださいカレンダーにイベントを追加するには、[リンク1](http://stackoverflow.com/a/14056064/6518860)[リンク2](のhttp://のstackoverflow。 com/a/28813990/6518860) –

+0

ありがとうございます。私のコードは最初のリンクと同じですが、2番目のコードも試してみます。 –

答えて

0

使用接触リゾルバは

ContentResolver cr = Env.currentActivity.getContentResolver(); 
        ContentValues[] eventsArray = new ContentValues[projectDataList.size()]; 
        for (int i = 0; i < projectDataList.size(); i++) { 
         projectDetailData = projectDataList.get(i); 
         ContentValues values = new ContentValues(); 
         TimeZone timeZone = TimeZone.getDefault(); 
         values.put(CalendarContract.Events.DTSTART, (long) (Util.dateToMiliSec(shift_start_date) + (Util.convertTimeIntoSecound(projectDetailData.shift_start_time, "HH:mm:ss") * 1000))); 
         values.put(CalendarContract.Events.DTEND, (long) (Util.dateToMiliSec(shift_end_date) + (Util.convertTimeIntoSecound(projectDetailData.shift_end_time, "HH:mm:ss") * 1000))); 
         values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID()); 
         values.put(CalendarContract.Events.TITLE, title); 
         values.put(CalendarContract.Events.DESCRIPTION, title); 
         values.put(Events.EVENT_LOCATION, work_location); 
         values.put(CalendarContract.Events.CALENDAR_ID, 1); 
         if (ActivityCompat.checkSelfPermission(Env.currentActivity, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) { 
          // TODO: Consider calling 
          // ActivityCompat#requestPermissions 
          // here to request the missing permissions, and then overriding 
          // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
          //           int[] grantResults) 
          // to handle the case where the user grants the permission. See the documentation 
          // for ActivityCompat#requestPermissions for more details. 
          return; 
         } 
         eventsArray[i] = values; 
        } 
        int a = cr.bulkInsert(CalendarContract.Events.CONTENT_URI, eventsArray); 
+0

ありがとうございますが、まだ動作していません。 –

+0

私にエラーとあなたのコードも送ってください –

+0

カレンダーに挿入していないだけのエラーはありません。 –

関連する問題