搜索
热搜: 活动 交友 discuz
查看: 4443|回复: 0
打印 上一主题 下一主题

Android 向系统日历中添加事件实例

[复制链接]

160

主题

165

帖子

814

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
814
跳转到指定楼层
楼主
发表于 2016-11-13 14:22:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
用户权限:


  1. 01.<uses-permission android:name="android.permission.READ_CALENDAR"/>   
  2. 02.<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
复制代码
XML布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     android:orientation="vertical" >

  7.     <TextView
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="wrap_content"
  10.         android:text="向系统日历中添加事件" />

  11.     <Button
  12.         android:id="@+id/inputaccount"
  13.         android:layout_width="fill_parent"
  14.         android:layout_height="wrap_content"
  15.         android:onClick="onClick"
  16.         android:text="添加账户" />
  17.    
  18.     <Button
  19.         android:id="@+id/readUserButton"
  20.         android:layout_width="fill_parent"
  21.         android:layout_height="wrap_content"
  22.         android:onClick="onClick"
  23.         android:text="查看账户" />

  24.     <Button
  25.         android:id="@+id/readEventButton"
  26.         android:layout_width="fill_parent"
  27.         android:layout_height="wrap_content"
  28.         android:onClick="onClick"
  29.         android:text="查看事件" />

  30.     <Button
  31.         android:id="@+id/writeEventButton"
  32.         android:layout_width="fill_parent"
  33.         android:layout_height="wrap_content"
  34.         android:onClick="onClick"
  35.         android:text="插入事件" />
  36.    
  37.     <Button
  38.         android:id="@+id/delEventButton"
  39.         android:layout_width="fill_parent"
  40.         android:layout_height="wrap_content"
  41.         android:onClick="onClick"
  42.         android:text="删除事件" />

  43. </LinearLayout>
复制代码


代码
  1. import java.util.Calendar;
  2. import java.util.TimeZone;

  3. import android.app.Activity;
  4. import android.content.ContentValues;
  5. import android.database.Cursor;
  6. import android.net.Uri;
  7. import android.os.Bundle;
  8. import android.provider.CalendarContract;
  9. import android.provider.CalendarContract.Calendars;
  10. import android.provider.CalendarContract.Events;
  11. import android.view.View;
  12. import android.widget.Toast;


  13. public class MainActivity extends Activity {

  14.     //Android2.2版本以后的URL,之前的就不写了
  15.     private static String calanderURL = "content://com.android.calendar/calendars";
  16.     private static String calanderEventURL = "content://com.android.calendar/events";
  17.     private static String calanderRemiderURL = "content://com.android.calendar/reminders";


  18.     @Override
  19.     public void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_main);
  22.         
  23.     }

  24.     public void onClick(View v) {
  25.         if (v.getId() == R.id.readUserButton) {  //读取系统日历账户,如果为0的话先添加
  26.             Cursor userCursor = getContentResolver().query(Uri.parse(calanderURL), null, null, null, null);
  27.             
  28.             System.out.println("Count: " + userCursor.getCount());            
  29.             Toast.makeText(this, "Count: " + userCursor.getCount(), Toast.LENGTH_LONG).show();   
  30.             
  31.             for (userCursor.moveToFirst(); !userCursor.isAfterLast(); userCursor.moveToNext()) {
  32.                 System.out.println("name: " + userCursor.getString(userCursor.getColumnIndex("ACCOUNT_NAME")));
  33.                
  34.                
  35.                 String userName1 = userCursor.getString(userCursor.getColumnIndex("name"));
  36.                 String userName0 = userCursor.getString(userCursor.getColumnIndex("ACCOUNT_NAME"));
  37.                 Toast.makeText(this, "NAME: " + userName1 + " -- ACCOUNT_NAME: " + userName0, Toast.LENGTH_LONG).show();
  38.             }
  39.         }
  40.         else if (v.getId() == R.id.inputaccount) {        //添加日历账户
  41.             initCalendars();
  42.             
  43.         }
  44.         else if (v.getId() == R.id.delEventButton) {  //删除事件
  45.    
  46.             int rownum = getContentResolver().delete(Uri.parse(calanderURL), "_id!=-1", null);  //注意:会全部删除所有账户,新添加的账户一般从id=1开始,
  47.                                                                                                   //可以令_id=你添加账户的id,以此删除你添加的账户   
  48.             Toast.makeText(MainActivity.this, "删除了: " + rownum, Toast.LENGTH_LONG).show();
  49.             
  50.         }
  51.         else if (v.getId() == R.id.readEventButton) {  //读取事件
  52.             Cursor eventCursor = getContentResolver().query(Uri.parse(calanderEventURL), null, null, null, null);
  53.             if (eventCursor.getCount() > 0) {
  54.                 eventCursor.moveToLast();             //注意:这里与添加事件时的账户相对应,都是向最后一个账户添加
  55.                 String eventTitle = eventCursor.getString(eventCursor.getColumnIndex("title"));
  56.                 Toast.makeText(MainActivity.this, eventTitle, Toast.LENGTH_LONG).show();
  57.             }
  58.         }
  59.         else if (v.getId() == R.id.writeEventButton) {
  60.             // 获取要出入的gmail账户的id
  61.             String calId = "";
  62.             Cursor userCursor = getContentResolver().query(Uri.parse(calanderURL), null, null, null, null);
  63.             if (userCursor.getCount() > 0) {
  64.                 userCursor.moveToLast();  //注意:是向最后一个账户添加,开发者可以根据需要改变添加事件 的账户
  65.                 calId = userCursor.getString(userCursor.getColumnIndex("_id"));
  66.             }
  67.             else {
  68.                 Toast.makeText(this, "没有账户,请先添加账户", 0).show();
  69.                 return;
  70.             }
  71.             
  72.             ContentValues event = new ContentValues();
  73.             event.put("title", "与苍井空小姐动作交流");
  74.             event.put("description", "Frankie受空姐邀请,今天晚上10点以后将在Sheraton动作交流.lol~");
  75.             // 插入账户
  76.             event.put("calendar_id", calId);
  77.             System.out.println("calId: " + calId);
  78.             event.put("eventLocation", "地球-华夏");   
  79.             
  80.             Calendar mCalendar = Calendar.getInstance();
  81.             mCalendar.set(Calendar.HOUR_OF_DAY, 11);
  82.             mCalendar.set(Calendar.MINUTE, 45);
  83.             long start = mCalendar.getTime().getTime();
  84.             mCalendar.set(Calendar.HOUR_OF_DAY, 12);
  85.             long end = mCalendar.getTime().getTime();

  86.             event.put("dtstart", start);
  87.             event.put("dtend", end);
  88.             event.put("hasAlarm", 1);

  89.             event.put(Events.EVENT_TIMEZONE, "Asia/Shanghai");  //这个是时区,必须有,
  90.             //添加事件
  91.             Uri newEvent = getContentResolver().insert(Uri.parse(calanderEventURL), event);        
  92.             //事件提醒的设定
  93.             long id = Long.parseLong(newEvent.getLastPathSegment());
  94.             ContentValues values = new ContentValues();
  95.             values.put("event_id", id);
  96.             // 提前10分钟有提醒
  97.             values.put("minutes", 10);
  98.             getContentResolver().insert(Uri.parse(calanderRemiderURL), values);
  99.             
  100.             Toast.makeText(MainActivity.this, "插入事件成功!!!", Toast.LENGTH_LONG).show();
  101.         }
  102.     }
  103.    

  104.     //添加账户
  105.     private void initCalendars() {

  106.         TimeZone timeZone = TimeZone.getDefault();
  107.         ContentValues value = new ContentValues();
  108.         value.put(Calendars.NAME, "yy");

  109.         value.put(Calendars.ACCOUNT_NAME, "mygmailaddress@gmail.com");
  110.         value.put(Calendars.ACCOUNT_TYPE, "com.android.exchange");
  111.         value.put(Calendars.CALENDAR_DISPLAY_NAME, "mytt");
  112.         value.put(Calendars.VISIBLE, 1);
  113.         value.put(Calendars.CALENDAR_COLOR, -9206951);
  114.         value.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
  115.         value.put(Calendars.SYNC_EVENTS, 1);
  116.         value.put(Calendars.CALENDAR_TIME_ZONE, timeZone.getID());
  117.         value.put(Calendars.OWNER_ACCOUNT, "mygmailaddress@gmail.com");
  118.         value.put(Calendars.CAN_ORGANIZER_RESPOND, 0);

  119.         Uri calendarUri = Calendars.CONTENT_URI;
  120.         calendarUri = calendarUri.buildUpon()
  121.                 .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
  122.                 .appendQueryParameter(Calendars.ACCOUNT_NAME, "mygmailaddress@gmail.com")
  123.                 .appendQueryParameter(Calendars.ACCOUNT_TYPE, "com.android.exchange")   
  124.                 .build();

  125.         getContentResolver().insert(calendarUri, value);
  126.     }
  127.    
  128.    
  129. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|安卓论坛  

GMT+8, 2024-4-29 18:31 , Processed in 0.080580 second(s), 36 queries .

Powered by Discuz! X3.2

© 2001-2013 Design S!|ƽ̶

快速回复 返回顶部 返回列表