2011-12-04 36 views
3

我正在使用以下代码在我的应用程序中获取日历活动。它在Android手机中工作正常,但是当我在Android平板电脑上试用此代码时,我的应用程序崩溃。所以我不知道到底是什么问题以及为什么它不适用于平板电脑。Android在平板电脑中获取日历活动

public void syncCalander() { 
    try { 

     nameValues = new ArrayList<NameValuePair>(); 
     StringBuffer calbuffers; 
     int cnt = 1; 
     StringBuffer calbufferimeis = new StringBuffer(); 

     ContentResolver contentResolver = getApplicationContext() 
     .getContentResolver(); 
     final Cursor cursor = contentResolver.query(
      Uri.parse("content://com.android.calendar/calendars"), 
      (new String[] { "_id", "displayName", "selected" }), null, 
      null, null); 
     if (cursor.getCount() == 0) { 

     } else { 

      HashSet<String> calendarIds = new HashSet<String>(); 
      CalendarModel calModel = new CalendarModel(); 

      CalendarModel.CALENDERLIST.add(calModel); 
      int val = cursor.getCount(); 
      Log.i("=============total event============>", "." + val); 
      while (cursor.moveToNext()) { 

       final String _id = cursor.getString(0); 
       final String displayName = cursor.getString(1); 
       final Boolean selected = !cursor.getString(2).equals("0"); 
       calModel.setCalendarEvent(displayName); 
       CalendarModel.CALENDERLIST.add(calModel); 
       Log.i("--------Display Name----------", "" + "Id: " + _id 
        + " Display Name: " + displayName + " Selected: " 
        + selected); 
       calendarIds.add(_id); 
       Log.i("============celenderIDs==========>", "." 
        + calendarIds); 
      } 

      for (String id : calendarIds) { 

       Uri.Builder builder = Uri.parse(
        "content://com.android.calendar/instances/when") 
       .buildUpon(); 
       long now = new Date().getTime(); 
       ContentUris.appendId(builder, now 
        - DateUtils.WEEK_IN_MILLIS); 
       ContentUris.appendId(builder, now 
        + DateUtils.WEEK_IN_MILLIS); 

       Cursor eventCursor = contentResolver.query(builder.build(), 
        new String[] { "title", "begin", "end", "allDay" }, 
        "Calendars._id=" + id, null, 
        "startDay ASC, startMinute ASC"); 
       Log.i("============cursor size===========>", "." 
        + eventCursor.getCount()); 

       while (eventCursor.moveToNext()) { 
        final String title = eventCursor.getString(0); 
        final Date begin = new Date(eventCursor.getLong(1)); 
        final Date end = new Date(eventCursor.getLong(2)); 
        final Boolean allDay = !eventCursor.getString(3) 
        .equals("0"); 
        calModel.setCalendarDate(begin.toString()); 
        CalendarModel.CALENDERLIST.add(calModel); 
        Log.i("-----Title--------", "Title: " + title 
         + " Begin: " + begin + " End: " + end 
         + " All Day: " + allDay); 
        SimpleDateFormat formatter = new SimpleDateFormat(
         "yyyy-MM-dd HH:mm:ss"); 
        String date = formatter.format(begin); 

        int callength = CalendarModel.CALENDERLIST.size(); 

        calbuffers = new StringBuffer(); 
        calbuffers.append("{\"Calenderevent\":\"" + title 
         + "\"," + "\"Calenderdate\":\"" + date + "\"}"); 

        calbuffers.append(","); 
        calbufferimeis.append(calbuffers); 

       } 
       eventCursor.close(); 
      } 

     } 
     cursor.close(); 
    } catch (Exception e) { 
    } 

} 
+1

你得到logcat的任何错误? –

回答

0

我有使用

content://calendar/calendars 

代替

content://com.android.calendar/calendars 

为Android平板电脑应用