日历中的帐户名称Android自动获取Gmail

时间:2014-08-28 12:18:39

标签: android android-calendar google-account

我发现这篇文章非常有用http://developer.android.com/guide/topics/providers/calendar-provider.html#query? 但在这里我发现日历中查询的ACCOUNT_NAME正在静态写入 我被困在这里因为我的问题是..如果我想要用户的gmail地址通过一些查询 即没有静态指定地址而是通过某些代码

PS:我是android的新手。如果这是一个愚蠢的问题,请道歉

long calID = 0;
        String displayName = null;
        String accountName = null;
        String ownerName = null;

        final String[] EVENT_PROJECTION = new String[] {
                Calendars._ID, // 0
                Calendars.ACCOUNT_NAME, // 1
                Calendars.CALENDAR_DISPLAY_NAME, // 2
                Calendars.OWNER_ACCOUNT // 3
        };

        // The indices for the projection array above.
        final int PROJECTION_ID_INDEX = 0;
        final int PROJECTION_ACCOUNT_NAME_INDEX = 1;
        final int PROJECTION_DISPLAY_NAME_INDEX = 2;
        final int PROJECTION_OWNER_ACCOUNT_INDEX = 3;

        // Run query
        Cursor cur = null;
        ContentResolver cr = app.getContentResolver();
        Uri uri = Calendars.CONTENT_URI;
        String selection = "((" + Calendars.ACCOUNT_NAME + " = ?) AND ("
                + Calendars.ACCOUNT_TYPE + " = ?) AND ("
                + Calendars.OWNER_ACCOUNT + " = ?))";
        String[] selectionArgs = new String[] { "sample@gmail.com",
                "com.google", "sample@gmail.com" };
        // Submit the query and get a Cursor object back.
        cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs,
                null);

        // Use the cursor to step through the returned records
        while (cur.moveToNext()) {


            // Get the field values
            calID = cur.getLong(PROJECTION_ID_INDEX);
            displayName = cur.getString(PROJECTION_DISPLAY_NAME_INDEX);
            accountName = cur.getString(PROJECTION_ACCOUNT_NAME_INDEX);
            ownerName = cur.getString(PROJECTION_OWNER_ACCOUNT_INDEX);
        }



        Calendar beginTime = Calendar.getInstance();
        Calendar ceaseTime = Calendar.getInstance();
        ContentValues cv = new ContentValues();

        HashMap<String, String> map = (HashMap<String, String>) params[0];

        String startDate[] = map.get("startDate").toString().split("-");
        String startTime[] = map.get("startTime").toString().split(":");
        long beginMilis=0,ceaseMilis=0;

        beginTime.set(Integer.parseInt(startDate[2]),
                Integer.parseInt(startDate[1]),
                Integer.parseInt(startDate[0]),
                Integer.parseInt(startTime[0]), 
                Integer.parseInt(startTime[1]));

        beginMilis = beginTime.getTimeInMillis();

        String endDate[] = map.get("endDate").toString().split("-");
        String endTime[] = map.get("endTime").toString().split(":");
        TimeZone tz = TimeZone.getDefault();

        ceaseTime.set(Integer.parseInt(endDate[2]),
                Integer.parseInt(endDate[1]),
                Integer.parseInt(endDate[0]),
                Integer.parseInt(endTime[0]),
                Integer.parseInt(endTime[1]));
        ceaseMilis = ceaseTime.getTimeInMillis();
        cv.put(Events.DTSTART, beginMilis);
        cv.put(Events.DTEND, ceaseMilis);
        cv.put(Events.TITLE, map.get("title").toString());
        cv.put(Events.DESCRIPTION, map.get("description").toString());
        cv.put(Events.CALENDAR_ID, String.valueOf(calID));
        cv.put(Events.EVENT_TIMEZONE, tz.getDisplayName());

        uri = cr.insert(Events.CONTENT_URI, cv);

0 个答案:

没有答案