2016-09-26 56 views
0

您好我已經用下面的代碼添加事件到設備的日曆,添加事件到設備的日曆中的棉花糖不工作的android

ContentResolver cr = this.getContentResolver(); 
     ContentValues values = new ContentValues(); 

     values.put(CalendarContract.Events.DTSTART, DateConversion.getMilliSecondFromString(eventStartDate)); 
     values.put(CalendarContract.Events.DTEND, DateConversion.getMilliSecondFromString(eventEndDate)); 
     values.put(CalendarContract.Events.TITLE, title); 
     values.put(CalendarContract.Events.DESCRIPTION, comment); 
     TimeZone timeZone = TimeZone.getDefault(); 
     values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID()); 
     values.put(CalendarContract.Events.CALENDAR_ID, 1); 
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) { 
    return; 
} 
     Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values); 
     if (uri!=null){ 
      Log.d(TAG, "event addEventToMyCalendar: Uri=>"+Long.valueOf(uri.getLastPathSegment())); 
      Toast.makeText(this,"Event Added To Calendar",Toast.LENGTH_SHORT).show(); 
     }else { 
      Log.d(TAG, "event addEventToMyCalendar: Uri=>Null"); 
     } 

和我的記錄值,

事件addToEventToMyCalendar:標題=>盧浮宮博物館=> eventStartDate => 2016-09-26 10:00:00 => comment =>在法國國王的前王室裏,盧浮宮是一座無與倫比的博物館,躋身歐洲頂級美術收藏之列。許多... => eventEndDate =的> 2016年9月26日12:00:00

事件addEventToMyCalendar:URI => 100

,但不是在我的設備的日曆顯示此事件。

此代碼正在低於棉花糖。但是這不適用於棉花糖。

我還爲WRITE_CALENDAR添加了運行時權限。

你能否建議我解決這個問題的任何想法。

回答

0
private void addToDeviceCalendar(String startDate,String endDate, String title,String description, String location) { 

     String stDate = startDate; 
     String enDate = endDate; 

     GregorianCalendar calDate = new GregorianCalendar(); 
     //GregorianCalendar calEndDate = new GregorianCalendar(); 

     SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
     SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy,MM,dd,HH,mm"); 
     Date date,edate; 
     try { 
      date = originalFormat.parse(startDate); 
      stDate=targetFormat.format(date); 

     } catch (ParseException ex) {} 

     long startMillis = 0; 
     long endMillis = 0; 
     String dates[] = stDate.split(","); 

     SD_YeaR = dates[0]; 
     SD_MontH = dates[1]; 
     SD_DaY = dates[2]; 
     SD_HouR = dates[3]; 
     SD_MinutE = dates[4]; 


     /*Log.e("YeaR ", SD_YeaR); 
     Log.e("MontH ",SD_MontH); 
     Log.e("DaY ", SD_DaY); 
     Log.e(" HouR", SD_HouR); 
     Log.e("MinutE ", SD_MinutE);*/ 

     calDate.set(Integer.parseInt(SD_YeaR), Integer.parseInt(SD_MontH)-1, Integer.parseInt(SD_DaY), Integer.parseInt(SD_HouR), Integer.parseInt(SD_MinutE)); 
     startMillis = calDate.getTimeInMillis(); 
/* 
     try { 
      edate = originalFormat.parse(endDate); 
      enDate=targetFormat.format(edate); 

     } catch (ParseException ex) {} 


     String end_dates[] = endDate.split(","); 

     String ED_YeaR = end_dates[0]; 
     String ED_MontH = end_dates[1]; 
     String ED_DaY = end_dates[2]; 

     String ED_HouR = end_dates[3]; 
     String ED_MinutE = end_dates[4]; 


     calEndDate.set(Integer.parseInt(ED_YeaR), Integer.parseInt(ED_MontH)-1, Integer.parseInt(ED_DaY), Integer.parseInt(ED_HouR), Integer.parseInt(ED_MinutE)); 
     endMillis = calEndDate.getTimeInMillis();*/ 

     try { 
      ContentResolver cr = getActivity().getContentResolver(); 
      ContentValues values = new ContentValues(); 
      values.put(CalendarContract.Events.DTSTART, startMillis); 
      values.put(CalendarContract.Events.DTEND, calDate.getTimeInMillis() + 60 * 60 * 1000); 
      values.put(CalendarContract.Events.TITLE, title); 
      values.put(CalendarContract.Events.DESCRIPTION, description); 
      values.put(CalendarContract.Events.EVENT_LOCATION,location); 
      values.put(CalendarContract.Events.HAS_ALARM,1); 
      values.put(CalendarContract.Events.CALENDAR_ID, 1); 
      values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance() 
        .getTimeZone().getID()); 
      System.out.println(Calendar.getInstance().getTimeZone().getID()); 
      if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) { 

       return; 
      } 
      Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values); 

      long eventId = Long.parseLong(uri.getLastPathSegment()); 
      Log.d("Ketan_Event_Id", String.valueOf(eventId)); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
+0

除了編寫代碼並留下它之外,通過添加對步驟的描述,可以更好地改進答案,以便實現目標。這將有助於讀者更好地理解你的代碼。沒有這個,你的答案就是長時間混淆代碼。 –

+1

試過這個,但仍然存在問題。獲取EventId,但事件不會顯示在我的設備日曆中。 – mustaq

相關問題