未添加日历活动

时间:2016-02-15 06:02:01

标签: android calendar

我有代码在android日历中添加调用事件。它工作得很好,但有时会在日历中调用未添加的事件。请参考我的代码,让我知道错误。

我在呼叫事件结束时添加了日历事件。

private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;
private static String savedNumber;  //because the passed incoming is only valid in ringing
private static String stateStr;
private static String cName;
private static CommonService commonService;
private static Context context;


@Override
public void onReceive(Context c, Intent intent) {
    context = c;        
    if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
        savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
    } else {
        stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
        String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
        int state = 0;
        if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
            state = TelephonyManager.CALL_STATE_IDLE;
        } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            state = TelephonyManager.CALL_STATE_OFFHOOK;
        } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            state = TelephonyManager.CALL_STATE_RINGING;
        }

        onCallStateChanged(context, state, number);
    }
}

public void onCallStateChanged(Context context, int state, String number) {
    if (lastState == state) {
        return;
    }


    if (state == TelephonyManager.CALL_STATE_RINGING) {
        isIncoming = true;
        callStartTime = new Date();        
        savedNumber = number;
    } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
        if (lastState != TelephonyManager.CALL_STATE_RINGING) {
            isIncoming = false;
            callStartTime = new Date();             
        }
    } else if (state == TelephonyManager.CALL_STATE_IDLE) {

        if (lastState == TelephonyManager.CALL_STATE_RINGING) {
            //Ring but no pickup-  a miss
            addCallLogInCalendar(LoggerConstants.MISSED, savedNumber, callStartTime, null, context);
        } else if (isIncoming) {
            // Toast.makeText(context, "CALL_ENDE", Toast.LENGTH_SHORT).show();
            addCallLogInCalendar(LoggerConstants.INCOMING, savedNumber, callStartTime, new Date(), context);
        } else {
            //onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());
            addCallLogInCalendar(LoggerConstants.OUTGOING, savedNumber, callStartTime, new Date(), context);
        }

    }
    lastState = state;
}

public void addCallLogInCalendar(String callType, String phNumber, Date sTime, Date eTime, Context context) {
    commonService = new CommonService();
    int calID = commonService.getCalendar(context);
    long startMillis = 0;
    long endMillis = 0;
    Calendar beginTime = Calendar.getInstance();
    beginTime.setTimeInMillis(sTime.getTime());
    startMillis = beginTime.getTimeInMillis();
    Calendar endTime = Calendar.getInstance();
    if (eTime != null) {
        endTime.setTimeInMillis(eTime.getTime());
    } else {
        endTime.setTimeInMillis(sTime.getTime());
    }
    endMillis = endTime.getTimeInMillis();
    ContentResolver cr = context.getContentResolver();
    ContentValues values = new ContentValues();
    values.put(CalendarContract.Events.DTSTART, startMillis);
    values.put(CalendarContract.Events.DTEND, endMillis);
    values.put(CalendarContract.Events.CALENDAR_ID, calID);       

    long duration = 0;
    if (eTime != null) {
        Long callTime = eTime.getTime() - sTime.getTime();
        duration = TimeUnit.MILLISECONDS.toMinutes(callTime);

    }
    cName = getContactName(context, phNumber);
    if (cName == null) {
        cName = phNumber;
    }


    values.put(CalendarContract.Events.TITLE, cName);

    if (callType.equals(LoggerConstants.OUTGOING)) {
        values.put(CalendarContract.Events.EVENT_COLOR, LoggerConstants.OUTGOING_CALL_COLOR);
        if (cName.equalsIgnoreCase(phNumber)) {
            values.put(CalendarContract.Events.DESCRIPTION, callType + " for " + cName);
        } else {
            values.put(CalendarContract.Events.DESCRIPTION, callType + " for " + cName + "(" + phNumber + ")");
        }
    } else if (callType.equals(LoggerConstants.INCOMING)) {

        values.put(CalendarContract.Events.EVENT_COLOR, LoggerConstants.INCOMING_CALL_COLOR);
        if (cName.equalsIgnoreCase(phNumber)) {
            values.put(CalendarContract.Events.DESCRIPTION, callType + " from " + cName);
        } else {
            values.put(CalendarContract.Events.DESCRIPTION, callType + " from " + cName + "(" + phNumber + ")");
        }
    } else {
        if (cName.equalsIgnoreCase(phNumber)) {
            values.put(CalendarContract.Events.DESCRIPTION, callType + " from " + cName);
        } else {
            values.put(CalendarContract.Events.DESCRIPTION, callType + " from " + cName + "(" + phNumber + ")");
        }
        values.put(CalendarContract.Events.EVENT_COLOR, LoggerConstants.MISSED_CALL_COLOR);

    }
    values.put(CalendarContract.Events.EVENT_TIMEZONE, CalendarContract.Calendars.CALENDAR_TIME_ZONE);       

    try {
        Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);        
    } catch (Exception e) {
        System.out.println("-----------------------Calendar Entry Error--------------------------------");
        System.out.println(e);

    }

}


private String getContactName(Context context, String phoneNumber) {
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cursor = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);

    try{
        if (cursor == null) {
            return null;
        }
        String contactName = null;
        if (cursor.moveToFirst()) {
            contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
        }

        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }

        return contactName;
    }finally {
        cursor.close();
    }

}


private String formatDate(Date d) {
    SimpleDateFormat sdf = new SimpleDateFormat(LoggerConstants.DATE_FORMAT);
    String currentData = sdf.format(d);
    return currentData;
}

先谢谢

0 个答案:

没有答案