当前位置: 代码迷 >> Android >> Android阶教程(二十三)之-Android中的日历读写操作!
  详细解决方案

Android阶教程(二十三)之-Android中的日历读写操作!

热度:97   发布时间:2016-05-01 17:16:52.0
Android高手进阶教程(二十三)之---Android中的日历读写操作!!!

大家好,好久没有更新blog了,今天给大家分享一下Android中一些自带日历的操作方法,这里主要用到了ContentProiver的知识.如果大家不明白ContentProvider建议先查一下资料,知道它是干什么的。这样更容易下面的例子.

好了废话不说,这里提个醒,Android中的日历,只有真机才有,模拟上是没有的,所以测试环境一定要真机!!

因为日历是系统自带的,所以我们读写它一定要申请权限,也就是在AndroidManifest.xml加如下两行代码(一个读一个写):

?

view plain
  1. <uses-permission?android:name="android.permission.READ_CALENDAR"/>???
  2. <uses-permission?android:name="android.permission.WRITE_CALENDAR"/>???

?

Android中日历用了三个URL,分别是日历用户的URL,事件的URL,事件提醒URL,三个URL在Android2.1之前是如下的样子:

?

view plain
  1. calanderURL?=?"content://calendar/calendars";??
  2. calanderEventURL?=?"content://calendar/events";??
  3. calanderRemiderURL=?"content://calendar/reminders";??

?

但是在Android2.2版本以后,三个URL有了改变,变成如下的样子:

?

view plain
  1. calanderURL?=?"content://com.android.calendar/calendars";??
  2. calanderEventURL?=?"content://com.android.calendar/events";??
  3. calanderRemiderURL?=?"content://com.android.calendar/reminders";??
??

?

还是老样子,为了让大家更好的理解,我写了一个简单的Demo,大家按照我的步骤一步一步的来:

第一步:新建一个Android工程命名为CalendarDemo.

第二步:修改main.xml布局文件,增加了三个按钮,代码如下:

?

view plain
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  3. ????android:orientation="vertical"??
  4. ????android:layout_width="fill_parent"??
  5. ????android:layout_height="fill_parent"??
  6. ????>??
  7. ???<TextView????
  8. ????android:layout_width="fill_parent"???
  9. ????????android:layout_height="wrap_content"???
  10. ????android:text="@string/hello"??
  11. ????/>??
  12. ???<Button??
  13. ????????android:id="@+id/readUserButton"??
  14. ????????android:layout_width="fill_parent"???
  15. ????android:layout_height="wrap_content"???
  16. ????android:text="Get?a?User"??
  17. ????/>?????
  18. ????<Button??
  19. ????????android:id="@+id/readEventButton"??
  20. ????????android:layout_width="fill_parent"???
  21. ????android:layout_height="wrap_content"???
  22. ????android:text="Get?a?Event"??
  23. ????/>??
  24. ?????<Button??
  25. ????????android:id="@+id/writeEventButton"??
  26. ????????android:layout_width="fill_parent"???
  27. ????android:layout_height="wrap_content"???
  28. ????android:text="Input?a?Event"??
  29. ????/>??
  30. </LinearLayout>??

?

第三步:修改主核心程序CalendarDemo.java,代码如下:

?

view plain
  1. package?com.tutor.calendardemo;??
  2. ??
  3. import?java.util.Calendar;??
  4. ??
  5. import?android.app.Activity;??
  6. import?android.content.ContentValues;??
  7. import?android.database.Cursor;??
  8. import?android.net.Uri;??
  9. import?android.os.Build;??
  10. import?android.os.Bundle;??
  11. import?android.view.View;??
  12. import?android.view.View.OnClickListener;??
  13. import?android.widget.Button;??
  14. import?android.widget.Toast;??
  15. ??
  16. public?class?CalendarDemo?extends?Activity?implements?OnClickListener?{??
  17. ????private?Button?mReadUserButton;??
  18. ????private?Button?mReadEventButton;??
  19. ????private?Button?mWriteEventButton;??
  20. ??????
  21. ????private?static?String?calanderURL?=?"";??
  22. ????private?static?String?calanderEventURL?=?"";??
  23. ????private?static?String?calanderRemiderURL?=?"";??
  24. ????//为了兼容不同版本的日历,2.2以后url发生改变??
  25. ????static{??
  26. ????????if(Integer.parseInt(Build.VERSION.SDK)?>=?8){??
  27. ????????????calanderURL?=?"content://com.android.calendar/calendars";??
  28. ????????????calanderEventURL?=?"content://com.android.calendar/events";??
  29. ????????????calanderRemiderURL?=?"content://com.android.calendar/reminders";??
  30. ??
  31. ????????}else{??
  32. ????????????calanderURL?=?"content://calendar/calendars";??
  33. ????????????calanderEventURL?=?"content://calendar/events";??
  34. ????????????calanderRemiderURL?=?"content://calendar/reminders";??????????
  35. ????????}??
  36. ????}??
  37. ????@Override??
  38. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  39. ????????super.onCreate(savedInstanceState);??
  40. ????????setContentView(R.layout.main);??
  41. ??????????
  42. ????????setupViews();??
  43. ????}??
  44. ??????
  45. ????private?void?setupViews(){??
  46. ????????mReadUserButton?=?(Button)findViewById(R.id.readUserButton);??
  47. ????????mReadEventButton?=?(Button)findViewById(R.id.readEventButton);??
  48. ????????mWriteEventButton?=?(Button)findViewById(R.id.writeEventButton);??
  49. ????????mReadUserButton.setOnClickListener(this);??
  50. ????????mReadEventButton.setOnClickListener(this);??
  51. ????????mWriteEventButton.setOnClickListener(this);??
  52. ????}??
  53. ??????
  54. ????@Override??
  55. ????public?void?onClick(View?v)?{??
  56. ????????if(v?==?mReadUserButton){??
  57. ??????????????
  58. ????????????Cursor?userCursor?=?getContentResolver().query(Uri.parse(calanderURL),?null,???
  59. ????????????????????null,?null,?null);??
  60. ????????????if(userCursor.getCount()?>?0){??
  61. ????????????????userCursor.moveToFirst();??
  62. ????????????????String?userName?=?userCursor.getString(userCursor.getColumnIndex("name"));??
  63. ????????????????Toast.makeText(CalendarDemo.this,?userName,?Toast.LENGTH_LONG).show();??
  64. ????????????}??
  65. ????????}else?if(v?==?mReadEventButton){??
  66. ????????????Cursor?eventCursor?=?getContentResolver().query(Uri.parse(calanderEventURL),?null,???
  67. ????????????????????null,?null,?null);??
  68. ????????????if(eventCursor.getCount()?>?0){??
  69. ????????????????eventCursor.moveToLast();??
  70. ????????????????String?eventTitle?=?eventCursor.getString(eventCursor.getColumnIndex("title"));??
  71. ????????????????Toast.makeText(CalendarDemo.this,?eventTitle,?Toast.LENGTH_LONG).show();??
  72. ????????????}??
  73. ????????}else?if(v?==?mWriteEventButton){??
  74. ????????????//获取要出入的gmail账户的id??
  75. ????????????String?calId?=?"";??
  76. ????????????Cursor?userCursor?=?getContentResolver().query(Uri.parse(calanderURL),?null,???
  77. ????????????????????null,?null,?null);??
  78. ????????????if(userCursor.getCount()?>?0){??
  79. ????????????????userCursor.moveToFirst();??
  80. ????????????????calId?=?userCursor.getString(userCursor.getColumnIndex("_id"));??
  81. ??????????????????
  82. ????????????}??
  83. ????????????ContentValues?event?=?new?ContentValues();??
  84. ????????????event.put("title",?"与苍井空小姐动作交流");??
  85. ????????????event.put("description",?"Frankie受空姐邀请,今天晚上10点以后将在Sheraton动作交流.lol~");??
  86. ????????????[email protected]??
  87. ????????????event.put("calendar_id",calId);??
  88. ??????????????
  89. ????????????Calendar?mCalendar?=?Calendar.getInstance();??
  90. ????????????mCalendar.set(Calendar.HOUR_OF_DAY,10);??
  91. ????????????long?start?=?mCalendar.getTime().getTime();??
  92. ????????????mCalendar.set(Calendar.HOUR_OF_DAY,11);??
  93. ????????????long?end?=?mCalendar.getTime().getTime();??
  94. ??????????????
  95. ????????????event.put("dtstart",?start);??
  96. ????????????event.put("dtend",?end);??
  97. ????????????event.put("hasAlarm",1);??
  98. ??????????????
  99. ????????????Uri?newEvent?=?getContentResolver().insert(Uri.parse(calanderEventURL),?event);??
  100. ????????????long?id?=?Long.parseLong(?newEvent.getLastPathSegment()?);??
  101. ????????????ContentValues?values?=?new?ContentValues();??
  102. ????????????values.put(?"event_id",?id?);??
  103. ????????????//提前10分钟有提醒??
  104. ????????????values.put(?"minutes",?10?);??
  105. ????????????getContentResolver().insert(Uri.parse(calanderRemiderURL),?values);??
  106. ????????????Toast.makeText(CalendarDemo.this,?"插入事件成功!!!",?Toast.LENGTH_LONG).show();??
  107. ????????}??
  108. ????}??
  109. }??
??

?

第四步:在AndroidManifest.xml中申请权限,代码如下:

?

view plain
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <manifest?xmlns:android="http://schemas.android.com/apk/res/android"??
  3. ??????package="com.tutor.calendardemo"??
  4. ??????android:versionCode="1"??
  5. ??????android:versionName="1.0">??
  6. ????<application?android:icon="@drawable/icon"?android:label="@string/app_name">??
  7. ????????<activity?android:name=".CalendarDemo"??
  8. ??????????????????android:label="@string/app_name">??
  9. ????????????<intent-filter>??
  10. ????????????????<action?android:name="android.intent.action.MAIN"?/>??
  11. ????????????????<category?android:name="android.intent.category.LAUNCHER"?/>??
  12. ????????????</intent-filter>??
  13. ????????</activity>??
  14. ????</application>??
  15. ????<uses-sdk?android:minSdkVersion="7"?/>??
  16. ????<uses-permission?android:name="android.permission.READ_CALENDAR"/>???
  17. ????<uses-permission?android:name="android.permission.WRITE_CALENDAR"/>???
  18. </manifest>???

?

第五步:运行上述Android工程,查看效果:

????????????????? 运行首界面??????????????????????????????????????????????????????获取登录账户名

?

???????????????????????? 获取事件title???????????????????????????????????????????? 插入一个事件

??

???????????????????? 查看日历多了一条事件?????????????????????????????????????? 查看事件详情

?

Ok今天就讲到这里,呵呵~

源代码下载地址:http://download.csdn.net/source/3004309

?

转自:http://blog.csdn.net/android_tutor/article/details/6165470

  相关解决方案