创建Android的pre ICS新的本地日历 [英] Create new local calendar in android pre ICS

查看:162
本文介绍了创建Android的pre ICS新的本地日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直struggelig对这个星期,所以现在我希望这里有人能给我一些清晰度。

I have been struggelig for weeks on this, so now I hope someone here can give me some clarity.

我的工作项目(pre ICS)包括登陆到我公司的服务器上,让我今后的工作安排和我的电话本存储在一个单独的日历。这个日历应该将它们在所有主要的日历应用(Jorte,业务日历,日历平稳++)浏览。
我得到了解析,并存储在手机上的SQLite数据库的计划。但我的斗争是从那里获得创建一个单独的本地日历和存储所有的事件出现。我以为我不得不为这个(不要乱了电话,与code运作良好,在其他帐户创建一个帐户:

The project I'm working on(pre ICS) includes logging on to my company's server, getting my future work schedule and store this on my phone in a seperate calendar. This calendar should them be viewable in all the major calendar apps (Jorte, Business Calendar, Smooth Calendar ++). I got the schedule parsed and stored on the phone in an SQLite database. But my struggle is getting from there to create a seperate local calendar and store all the events there. I assumed I had to create an account for this (not to mess up any other account on the phone. That worked well with the code:

AccountManager man = (AccountManager) getSystemService(ACCOUNT_SERVICE);
Account acc = new Account("myCalendar", "com.lumabyte.mycalendar");

但我认为建立该帐户日历将相当容易:

But I thought creating a calendar on that account would be quite easy with:

ContentValues calendar = new ContentValues();
calendar.put("_sync_account", "myCalendar"); // My account
calendar.put("_sync_account_type","com.lumabyte.mycalendar"); 
calendar.put("name", "myCalendar");
calendar.put("displayName", "myCalendar");
calendar.put("hidden",0);
calendar.put("color",0xFF008080);
calendar.put("access_level", 700);
calendar.put("sync_events", 1);
calendar.put("timezone", "Europe/Paris");
calendar.put("ownerAccount", sync_account);
Uri calendarUri = Uri.parse(getCalendarUriBase() + "calendars");
this.getContentResolver().insert(calendarUri, calendar);

函数getCalendarUriBase():

The function getCalendarUriBase():

private String getCalendarUriBase() {

    String calendarUriBase = null;
    Uri calendars = Uri.parse("content://calendar/calendars");
    Cursor managedCursor = null;
    try {
        managedCursor = this.managedQuery(calendars, null, null, null, null);
    } catch (Exception e) {
        // eat
    }

    if (managedCursor != null) {
        calendarUriBase = "content://calendar/";
    } else {
        calendars = Uri.parse("content://com.android.calendar/calendars");
        try {
            managedCursor = this.managedQuery(calendars, null, null, null, null);
        } catch (Exception e) {
            // eat
        }

        if (managedCursor != null) {
            calendarUriBase = "content://com.android.calendar/";
        }

    }

    managedCursor.close();

    return calendarUriBase;
}

清单包括:

  <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
  <uses-permission android:name="android.permission.USE_CREDENTIALS" /> 
  <uses-permission android:name="android.permission.MAMAGE_ACCOUNTS"/>
  <uses-permission android:name="android.permission.GET_ACCOUNTS" />    
  <uses-permission android:name="android.permission.READ_CALENDAR">
  <uses-permission android:name="android.permission.WRITE_CALENDAR">    

和我authenticator.xml是这样的:

and my authenticator.xml is like this:

<?xml version="1.0" encoding="utf-8"?>
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"  
    android:accountType="com.lumabyte.mycalendar"
    android:icon="@drawable/icon"
    android:smallIcon="@drawable/icon"
    android:label="@string/authenticator_label"
/>

我没有得到任何错误消息,但只有创建帐户,而不是日历。有没有在code一个小错误,还是我误解了整个概念。我知道有没有API pre ICS,并且有有风险。但我需要它无论如何做。 $ P $尽可能多的手机尽可能pfeably稳定。任何帮助或例子会大大AP preciated。

I do not get any error messages, but only the account is created, and not the calendar. Is there a small error in the code, or have I misunderstood the whole concept. I know there is no API pre ICS, and that there is risks involved. But I need it done anyway. Prefeably stable on as many phones as possible. Any help or examples would be greatly appreciated.

我将与ICS后来因为我知道他们现在有我的公共API的这项工作。

I'll work with ICS later as I understand they now have i public API for that.

研究

推荐答案

HIII舵.............我在2.3.6使用后大量以下code添加日历中的R和D .........

hiii rudder............. I have add a calender in 2.3.6 using the following code after lot of R&D.........

ContentValues calendar=new ContentValues();
Uri calendarUri =Uri.parse(getBaseCalUri() + "/calendars");

calendar.put("_sync_account", "Test1");
calendar.put("_sync_account_type", "LOCAL");

    calendar.put("name", "CalenTest");
    calendar.put("displayName", "calDispName");
    calendar.put("color", 0xFF008080);
    calendar.put("access_level", 700);
    calendar.put("ownerAccount", true);
    calendar.put("sync_events", 1);

    calendarUri = calendarUri.buildUpon()
        .appendQueryParameter("_sync_account", "Test1")
        .appendQueryParameter("_sync_account_type", "LOCAL")  
        .build();
    Uri result = getContentResolver().insert(calendarUri, calendar);

和我getBaseCalUri()是

and my getBaseCalUri() is

private Uri getBaseCalUri(){
        Class<?> calendarProviderClass;
        Field uriField;
        Uri calendarUri=null;
        try {
            calendarProviderClass = Class.forName("android.provider.Calendar");
            uriField = calendarProviderClass.getField("CONTENT_URI");
            calendarUri = (Uri) uriField.get(null);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return calendarUri;
    }

试试这个....

try this....

这篇关于创建Android的pre ICS新的本地日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆