分享错误,学习生活

Android 向系统日历中添加提醒(踩坑)

52yxgame

代码:

  1. import android.app.Activity;
  2. import android.content.ContentValues;
  3. import android.database.Cursor;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.provider.CalendarContract;
  7. import android.provider.CalendarContract.Calendars;
  8. import android.view.View;
  9. import android.widget.Toast;
  10. import java.util.Calendar;
  11. import java.util.TimeZone;
  12. public class MainActivity extends Activity {
  13. //Android2.2版本以后的URL,之前的就不写了
  14. private static String calanderURL = ;
  15. private static String calanderEventURL = ;
  16. private static String calanderRemiderURL = ;
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. }
  22. public void onClick(View v) {
  23. if (v.getId() == R.id.readUserButton) { //读取系统日历账户,如果为0的话先添加
  24. Cursor userCursor = getContentResolver().query(Uri.parse(calanderURL), null, null, null, null);
  25. System.out.println( + userCursor.getCount());
  26. Toast.makeText(this, + userCursor.getCount(), Toast.LENGTH_LONG).show();
  27. for (userCursor.moveToFirst(); !userCursor.isAfterLast(); userCursor.moveToNext()) {
  28. System.out.println( + userCursor.getString(userCursor.getColumnIndex()));
  29. String userName1 = userCursor.getString(userCursor.getColumnIndex());
  30. String userName0 = userCursor.getString(userCursor.getColumnIndex());
  31. Toast.makeText(this, + userName1 + + userName0, Toast.LENGTH_LONG).show();
  32. }
  33. } else if (v.getId() == R.id.inputaccount) { //添加日历账户
  34. initCalendars();
  35. } else if (v.getId() == R.id.delEventButton) { //删除事件
  36. int rownum = getContentResolver().delete(Uri.parse(calanderURL), , null); //注意:会全部删除所有账户,新添加的账户一般从id=1开始,
  37. //可以令_id=你添加账户的id,以此删除你添加的账户
  38. Toast.makeText(MainActivity.this, + rownum, Toast.LENGTH_LONG).show();
  39. } else if (v.getId() == R.id.readEventButton) { //读取事件
  40. Cursor eventCursor = getContentResolver().query(Uri.parse(calanderEventURL), null, null, null, null);
  41. if (eventCursor.getCount() > 0) {
  42. eventCursor.moveToLast(); //注意:这里与添加事件时的账户相对应,都是向最后一个账户添加
  43. String eventTitle = eventCursor.getString(eventCursor.getColumnIndex());
  44. Toast.makeText(MainActivity.this, eventTitle, Toast.LENGTH_LONG).show();
  45. }
  46. } else if (v.getId() == R.id.writeEventButton) {
  47. // 获取要出入的gmail账户的id
  48. String calId = ;
  49. Cursor userCursor = getContentResolver().query(Uri.parse(calanderURL), null, null, null, null);
  50. if (userCursor.getCount() > 0) {
  51. userCursor.moveToLast(); //注意:是向最后一个账户添加,开发者可以根据需要改变添加事件 的账户
  52. calId = userCursor.getString(userCursor.getColumnIndex());
  53. } else {
  54. Toast.makeText(this, , Toast.LENGTH_LONG).show();
  55. return;
  56. }
  57. ContentValues event = new ContentValues();
  58. event.put(, );
  59. event.put(, );
  60. // 插入账户
  61. event.put(, calId);
  62. System.out.println( + calId);
  63. event.put(, );
  64. Calendar mCalendar = Calendar.getInstance();
  65. mCalendar.set(Calendar.HOUR_OF_DAY, 14);
  66. mCalendar.set(Calendar.MINUTE, 53);
  67. long start = mCalendar.getTime().getTime();
  68. mCalendar.set(Calendar.HOUR_OF_DAY, 15);
  69. long end = mCalendar.getTime().getTime();
  70. event.put(CalendarContract.Events.DTSTART, start);//开始时间
  71. event.put(CalendarContract.Events.DTEND, end);//结束时间
  72. //设置一个全天事件的条目
  73. //事件状态暂定(0),确认(1)或取消(2)
  74. //控制是否事件触发报警,提醒如下
  75. event.put(CalendarContract.Events.HAS_ALARM, 1); // 0 for false, 1 for true
  76. //设置时区,否则会报错
  77. event.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
  78. //添加事件
  79. Uri newEvent = getContentResolver().insert(Uri.parse(calanderEventURL), event);
  80. //事件提醒的设定
  81. long id = Long.parseLong(newEvent.getLastPathSegment());
  82. ContentValues values = new ContentValues();
  83. values.put(CalendarContract.Reminders.EVENT_ID, id);
  84. // 提前5分钟有提醒(提前0分钟时,值为0)
  85. values.put(CalendarContract.Reminders.MINUTES, 5);
  86. values.put(CalendarContract.Reminders.METHOD, 1);
  87. Uri uri = getContentResolver().insert(Uri.parse(calanderRemiderURL), values);
  88. if (uri == null) {
  89. // 添加闹钟提醒失败直接返回
  90. Toast.makeText(MainActivity.this, , Toast.LENGTH_LONG).show();
  91. return;
  92. }
  93. Toast.makeText(MainActivity.this, , Toast.LENGTH_LONG).show();
  94. }
  95. }
  96. //添加账户
  97. private void initCalendars() {
  98. TimeZone timeZone = TimeZone.getDefault();
  99. ContentValues value = new ContentValues();
  100. value.put(Calendars.NAME, );
  101. value.put(Calendars.ACCOUNT_NAME, );
  102. value.put(Calendars.ACCOUNT_TYPE, );
  103. value.put(Calendars.CALENDAR_DISPLAY_NAME, );
  104. value.put(Calendars.VISIBLE, 1);
  105. value.put(Calendars.CALENDAR_COLOR, -9206951);
  106. value.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
  107. value.put(Calendars.SYNC_EVENTS, 1);
  108. value.put(Calendars.CALENDAR_TIME_ZONE, timeZone.getID());
  109. value.put(Calendars.OWNER_ACCOUNT, );
  110. value.put(Calendars.CAN_ORGANIZER_RESPOND, 0);
  111. Uri calendarUri = Calendars.CONTENT_URI;
  112. calendarUri = calendarUri.buildUpon()
  113. .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, )
  114. .appendQueryParameter(Calendars.ACCOUNT_NAME, )
  115. .appendQueryParameter(Calendars.ACCOUNT_TYPE, )
  116. .build();
  117. getContentResolver().insert(calendarUri, value);
  118. }
  119. }

布局文件:

  1. <?xml version= encoding=?>
  2. <LinearLayout xmlns:android=
  3. android:layout_width=
  4. android:layout_height=
  5. android:orientation=>
  6. <TextView
  7. android:layout_width=
  8. android:layout_height=
  9. android:text= />
  10. <Button
  11. android:id=
  12. android:layout_width=
  13. android:layout_height=
  14. android:onClick=
  15. android:text= />
  16. <Button
  17. android:id=
  18. android:layout_width=
  19. android:layout_height=
  20. android:onClick=
  21. android:text= />
  22. <Button
  23. android:id=
  24. android:layout_width=
  25. android:layout_height=
  26. android:onClick=
  27. android:text= />
  28. <Button
  29. android:id=
  30. android:layout_width=
  31. android:layout_height=
  32. android:onClick=
  33. android:text= />
  34. <Button
  35. android:id=
  36. android:layout_width=
  37. android:layout_height=
  38. android:onClick=
  39. android:text= />
  40. </LinearLayout>

需要添加的权限:

  1. <uses-permission android:name=/>
  2. <uses-permission android:name=/>

这里说一下有两个地方需要注意:
1、设置提前几分钟提醒时,如果是准时提醒的话需要设置为0

  1. // 提前5分钟有提醒(提前0分钟时,值为0)
  2. values.put(CalendarContract.Reminders.MINUTES, 5);

2、需要设置 提醒的次数

values.put(CalendarContract.Reminders.METHOD, 1);

52yxgame
nmei\u7f8e\u5973\u56fe\u5e93

暂无评论

发表评论

电子邮件地址不会被公开。