2013-04-14 160 views
0

我爲事件添加以下代碼:添加事件到Android日曆不顯示日曆

Intent calendarIntent = new Intent(Intent.ACTION_INSERT, Events.CONTENT_URI); 

Calendar beginTime = Calendar.getInstance(); 
beginTime.set(2013,04,14,21,00,00); 

Calendar endTime = Calendar.getInstance(); 
endTime.set(2013,04,14,21,10,00); 
Intent intent = new Intent(Intent.ACTION_EDIT); 
intent.setType("vnd.android.cursor.item/event"); 
intent.putExtra("title", "Some title"); 
intent.putExtra("description", "Some description"); 
intent.putExtra("beginTime", beginTime.getTimeInMillis()); 
intent.putExtra("endTime", endTime.getTimeInMillis()); 
startActivity(intent); 

但如何插入事件,而不打開日曆?

感謝

回答

1

你需要使用一個ContentResolver代替Intent

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

Calendar beginTime = Calendar.getInstance(); 
beginTime.set(2013,04,14,21,00,00); 

Calendar endTime = Calendar.getInstance(); 
endTime.set(2013,04,14,21,10,00); 

values.put(Events.DTSTART, beginTime.getTimeInMillis()); 
values.put(Events.DTEND, endTime.getTimeInMillis()); 
values.put(Events.TITLE, "Some title"); 
values.put(Events.DESCRIPTION, "Some description"); 

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

你可以閱讀更多Calendar Provider

+0

感謝您的幫助,但我在這條線遇到錯誤:開放的URI = CR。插入(Events.CONTENT_URI,values); – Gold

+0

你的api必須是14+ – rkmax

+0

我的api是14+,並且在Uri uri = cr.insert(Events.CONTENT_URI,values)上仍然出錯。 – Gold