• Android中的日历读写操作!!! (转)


    转载自:Android高手进阶教程(二十三)之---Android中的日历读写操作!!! - Android_Tutor的专栏 - CSDN博客

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

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

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

    view plaincopy to clipboardprint?
    <uses-permission android:name="android.permission.READ_CALENDAR"/>   
    <uses-permission android:name="android.permission.WRITE_CALENDAR"/>  
    <uses-permission android:name="android.permission.READ_CALENDAR"/>
    <uses-permission android:name="android.permission.WRITE_CALENDAR"/> 

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

    view plaincopy to clipboardprint?
    calanderURL = "content://calendar/calendars";  
    calanderEventURL = "content://calendar/events";  
    calanderRemiderURL= "content://calendar/reminders"; 
    calanderURL = "content://calendar/calendars";
    calanderEventURL = "content://calendar/events";
    calanderRemiderURL= "content://calendar/reminders";

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

    view plaincopy to clipboardprint?
    calanderURL = "content://com.android.calendar/calendars";  
    calanderEventURL = "content://com.android.calendar/events";  
    calanderRemiderURL = "content://com.android.calendar/reminders"; 
    calanderURL = "content://com.android.calendar/calendars";
    calanderEventURL = "content://com.android.calendar/events";
    calanderRemiderURL = "content://com.android.calendar/reminders"; 

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

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

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

    view plaincopy to clipboardprint?
    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        >  
       <TextView    
        android:layout_width="fill_parent"   
            android:layout_height="wrap_content"   
        android:text="@string/hello" 
        />  
       <Button  
            android:id="@+id/readUserButton" 
            android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
        android:text="Get a User" 
        />     
        <Button  
            android:id="@+id/readEventButton" 
            android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
        android:text="Get a Event" 
        />  
         <Button  
            android:id="@+id/writeEventButton" 
            android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
        android:text="Input a Event" 
        />  
    </LinearLayout> 
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
       <TextView 
        android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        android:text="@string/hello"
        />
       <Button
            android:id="@+id/readUserButton"
            android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Get a User"
        />  
        <Button
            android:id="@+id/readEventButton"
            android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Get a Event"
        />
         <Button
             android:id="@+id/writeEventButton"
            android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Input a Event"
        />
    </LinearLayout>

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

    view plaincopy to clipboardprint?
    package com.tutor.calendardemo;  
    import java.util.Calendar;  
    import android.app.Activity;  
    import android.content.ContentValues;  
    import android.database.Cursor;  
    import android.net.Uri;  
    import android.os.Build;  
    import android.os.Bundle;  
    import android.view.View;  
    import android.view.View.OnClickListener;  
    import android.widget.Button;  
    import android.widget.Toast;  
    public class CalendarDemo extends Activity implements OnClickListener {  
        private Button mReadUserButton;  
        private Button mReadEventButton;  
        private Button mWriteEventButton;  
        private static String calanderURL = "";  
        private static String calanderEventURL = "";  
        private static String calanderRemiderURL = "";  
        //为了兼容不同版本的日历,2.2以后url发生改变  
        static{  
            if(Integer.parseInt(Build.VERSION.SDK) >= 8){  
                calanderURL = "content://com.android.calendar/calendars";  
                calanderEventURL = "content://com.android.calendar/events";  
                calanderRemiderURL = "content://com.android.calendar/reminders";  
            }else{  
                calanderURL = "content://calendar/calendars";  
                calanderEventURL = "content://calendar/events";  
                calanderRemiderURL = "content://calendar/reminders";          
            }  
        }  
        @Override 
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);  
            setupViews();  
        }  
        private void setupViews(){  
            mReadUserButton = (Button)findViewById(R.id.readUserButton);  
            mReadEventButton = (Button)findViewById(R.id.readEventButton);  
            mWriteEventButton = (Button)findViewById(R.id.writeEventButton);  
            mReadUserButton.setOnClickListener(this);  
            mReadEventButton.setOnClickListener(this);  
            mWriteEventButton.setOnClickListener(this);  
        }  
        @Override 
        public void onClick(View v) {  
            if(v == mReadUserButton){  
                Cursor userCursor = getContentResolver().query(Uri.parse(calanderURL), null,   
                        null, null, null);  
                if(userCursor.getCount() > 0){  
                    userCursor.moveToFirst();  
                    String userName = userCursor.getString(userCursor.getColumnIndex("name"));  
                    Toast.makeText(CalendarDemo.this, userName, Toast.LENGTH_LONG).show();  
                }  
            }else if(v == mReadEventButton){  
                Cursor eventCursor = getContentResolver().query(Uri.parse(calanderEventURL), null,   
                        null, null, null);  
                if(eventCursor.getCount() > 0){  
                    eventCursor.moveToLast();  
                    String eventTitle = eventCursor.getString(eventCursor.getColumnIndex("title"));  
                    Toast.makeText(CalendarDemo.this, eventTitle, Toast.LENGTH_LONG).show();  
                }  
            }else if(v == mWriteEventButton){  
                //获取要出入的gmail账户的id  
                String calId = "";  
                Cursor userCursor = getContentResolver().query(Uri.parse(calanderURL), null,   
                        null, null, null);  
                if(userCursor.getCount() > 0){  
                    userCursor.moveToFirst();  
                    calId = userCursor.getString(userCursor.getColumnIndex("_id"));  
                }  
                ContentValues event = new ContentValues();  
                event.put("title", "与苍井空小姐动作交流");  
                event.put("description", "Frankie受空姐邀请,今天晚上10点以后将在Sheraton动作交流.lol~");  
                //插入hoohbood@gmail.com这个账户  
                event.put("calendar_id",calId);  
                Calendar mCalendar = Calendar.getInstance();  
                mCalendar.set(Calendar.HOUR_OF_DAY,10);  
                long start = mCalendar.getTime().getTime();  
                mCalendar.set(Calendar.HOUR_OF_DAY,11);  
                long end = mCalendar.getTime().getTime();  
                event.put("dtstart", start);  
                event.put("dtend", end);  
                event.put("hasAlarm",1);  
                Uri newEvent = getContentResolver().insert(Uri.parse(calanderEventURL), event);  
                long id = Long.parseLong( newEvent.getLastPathSegment() );  
                ContentValues values = new ContentValues();  
                values.put( "event_id", id );  
                //提前10分钟有提醒  
                values.put( "minutes", 10 );  
                getContentResolver().insert(Uri.parse(calanderRemiderURL), values);  
                Toast.makeText(CalendarDemo.this, "插入事件成功!!!", Toast.LENGTH_LONG).show();  
            }  
        }  

    package com.tutor.calendardemo;

    import java.util.Calendar;

    import android.app.Activity;
    import android.content.ContentValues;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Build;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;

    public class CalendarDemo extends Activity implements OnClickListener {
        private Button mReadUserButton;
        private Button mReadEventButton;
        private Button mWriteEventButton;
        private static String calanderURL = "";
        private static String calanderEventURL = "";
        private static String calanderRemiderURL = "";
        //为了兼容不同版本的日历,2.2以后url发生改变
        static{
            if(Integer.parseInt(Build.VERSION.SDK) >= 8){
                calanderURL = "content://com.android.calendar/calendars";
                calanderEventURL = "content://com.android.calendar/events";
                calanderRemiderURL = "content://com.android.calendar/reminders";

            }else{
                calanderURL = "content://calendar/calendars";
                calanderEventURL = "content://calendar/events";
                calanderRemiderURL = "content://calendar/reminders";       
            }
        }
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            setupViews();
        }
        private void setupViews(){
            mReadUserButton = (Button)findViewById(R.id.readUserButton);
            mReadEventButton = (Button)findViewById(R.id.readEventButton);
            mWriteEventButton = (Button)findViewById(R.id.writeEventButton);
            mReadUserButton.setOnClickListener(this);
            mReadEventButton.setOnClickListener(this);
            mWriteEventButton.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            if(v == mReadUserButton){
                Cursor userCursor = getContentResolver().query(Uri.parse(calanderURL), null,
                        null, null, null);
                if(userCursor.getCount() > 0){
                    userCursor.moveToFirst();
                    String userName = userCursor.getString(userCursor.getColumnIndex("name"));
                    Toast.makeText(CalendarDemo.this, userName, Toast.LENGTH_LONG).show();
                }
            }else if(v == mReadEventButton){
                Cursor eventCursor = getContentResolver().query(Uri.parse(calanderEventURL), null,
                        null, null, null);
                if(eventCursor.getCount() > 0){
                    eventCursor.moveToLast();
                    String eventTitle = eventCursor.getString(eventCursor.getColumnIndex("title"));
                    Toast.makeText(CalendarDemo.this, eventTitle, Toast.LENGTH_LONG).show();
                }
            }else if(v == mWriteEventButton){
                //获取要出入的gmail账户的id
                String calId = "";
                Cursor userCursor = getContentResolver().query(Uri.parse(calanderURL), null,
                        null, null, null);
                if(userCursor.getCount() > 0){
                    userCursor.moveToFirst();
                    calId = userCursor.getString(userCursor.getColumnIndex("_id"));
                }
                ContentValues event = new ContentValues();
                event.put("title", "与苍井空小姐动作交流");
                event.put("description", "Frankie受空姐邀请,今天晚上10点以后将在Sheraton动作交流.lol~");
                //插入hoohbood@gmail.com这个账户
                event.put("calendar_id",calId);
                Calendar mCalendar = Calendar.getInstance();
                mCalendar.set(Calendar.HOUR_OF_DAY,10);
                long start = mCalendar.getTime().getTime();
                mCalendar.set(Calendar.HOUR_OF_DAY,11);
                long end = mCalendar.getTime().getTime();
                event.put("dtstart", start);
                event.put("dtend", end);
                event.put("hasAlarm",1);
                Uri newEvent = getContentResolver().insert(Uri.parse(calanderEventURL), event);
                long id = Long.parseLong( newEvent.getLastPathSegment() );
                ContentValues values = new ContentValues();
                values.put( "event_id", id );
                //提前10分钟有提醒
                values.put( "minutes", 10 );
                getContentResolver().insert(Uri.parse(calanderRemiderURL), values);
                Toast.makeText(CalendarDemo.this, "插入事件成功!!!", Toast.LENGTH_LONG).show();
            }
        }

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

    view plaincopy to clipboardprint?
    <?xml version="1.0" encoding="utf-8"?>  
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.tutor.calendardemo" 
          android:versionCode="1" 
          android:versionName="1.0">  
        <application android:icon="@drawable/icon" android:label="@string/app_name">  
            <activity android:name=".CalendarDemo" 
                      android:label="@string/app_name">  
                <intent-filter>  
                    <action android:name="android.intent.action.MAIN" />  
                    <category android:name="android.intent.category.LAUNCHER" />  
                </intent-filter>  
            </activity>  
        </application>  
        <uses-sdk android:minSdkVersion="7" />  
        <uses-permission android:name="android.permission.READ_CALENDAR"/>   
        <uses-permission android:name="android.permission.WRITE_CALENDAR"/>   
    </manifest>  
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.tutor.calendardemo"
          android:versionCode="1"
          android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".CalendarDemo"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
        <uses-sdk android:minSdkVersion="7" />
        <uses-permission android:name="android.permission.READ_CALENDAR"/>
        <uses-permission android:name="android.permission.WRITE_CALENDAR"/>
    </manifest> 

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

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

                             获取事件title                                             插入一个事件

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

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

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

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Android_Tutor/archive/2011/01/26/6165470.aspx

  • 相关阅读:
    致远OA-A8协同管理软件无需登录getshell漏洞
    WordPress插件NextGEN Gallery <= 3.2.2 RCE漏洞利用(需要登录)
    Coremail邮件系统配置文件泄露漏洞
    CVE-2019-12735 VIM命令执行漏洞利用
    分享一个乌云Drops文章在线浏览的网站
    Ubuntu、Debian安装Docker CE
    一些好用的网络渗透工具和查询平台
    CVE-2017-11882 Office内存损坏漏洞利用
    Cobalt Strike入门教程-通过exe木马实现远控
    Jfinal cms前台评论XSS漏洞分析
  • 原文地址:https://www.cnblogs.com/zjmsky/p/1963847.html
Copyright © 2020-2023  润新知