我知道,我知道,在我的android清单中我已经声明了权限!但也许是在错误的地方?
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
//Lots of activities here
</application>
然而,它仍然给我一个错误,说许可被拒绝,你需要获得android.permission.READ_CALENDAR或WRITE_CALENDAR。我正在尝试将事件插入默认日历。以下是我的代码:
Calendar begin = Calendar.getInstance();
begin.set(dp.getYear(), dp.getMonth(), dp.getDayOfMonth(), tp.getHour(), tp.getMinute());
long beginMillis = begin.getTimeInMillis();
long endMillis = beginMillis + 1800000;
String eventUriString = "content://com.android.calendar/events";
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, beginMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.TITLE, "Flashcards Review: " + deckName);
values.put(Events.CALENDAR_ID, 1);
values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
Uri eventUri = cr.insert(Uri.parse(eventUriString), values);
我无法找到一个好的教程,所以我不知道这段代码是否有意义......对不起!请帮忙!
答案 0 :(得分:1)
日历
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,cal.getTimeInMillis());
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,cal.getTimeInMillis()+60*60*1000);
intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);
intent.putExtra(Events.TITLE, "Event title here");
intent.putExtra(Events.DESCRIPTION, "This is a sample description");
intent.putExtra(Events.EVENT_LOCATION, "Event Address");
intent.putExtra(Events.RRULE, "FREQ=YEARLY");
startActivity(intent);
并在 Manifest.xml 中使用权限,如下所示
<!-- Required to add events to Calendar -->
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
希望此代码能为您提供帮助:)