我是初学Android开发者。我写了一些简单的应用程序,女巫添加到关于调用的日历信息。现在我想添加自动更新,如果有人给我打电话,它会自动更新日历。
源代码:
DateFormat date = new SimpleDateFormat("dd/MM/yyyy hh:mm");
String[] projection = new String[] { "_id", "name" };
ContentValues event = new ContentValues();
long currentTime = System.currentTimeMillis();
Cursor callCur = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI, null, null, null, android.provider.CallLog.Calls.DATE + " DESC");
Uri calendarsUri = Uri.parse("content://com.android.calendar/calendars");
Cursor managedCursor = managedQuery(calendarsUri, projection, "selected=1", null, null);
int typeOfCall = 0;
String callCachedName = null;
String callName = null;
int duration = 0;
String compareNumbers = null;
while(callCur.moveToNext()){
compareNumbers = callCur.getString(callCur.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
int callDateIn = callCur.getColumnIndexOrThrow(android.provider.CallLog.Calls.DATE);
long callDateLong = new Long(callCur.getLong(callDateIn));
event.put("calendar_id", 2);
typeOfCall = callCur.getInt(callCur.getColumnIndex(android.provider.CallLog.Calls.TYPE));
Log.d(TAG, "Type of Call: " + typeOfCall);
if (typeOfCall == 1){
duration = callCur.getInt(callCur.getColumnIndex(android.provider.CallLog.Calls.DURATION));
Log.d(TAG, "Duration: " + duration);
if (duration == 0){
event.put("description", "Number wasn't avaibled");
}
else{
event.put("description", "Duration of call was: " + duration);
}
if (compareNumbers == null){
callName = callCur.getString(callCur.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
Log.d(TAG, "callName: " + callName);
event.put("title", "Called to: " + callName);
}
else {
callCachedName = callCur.getString(callCur.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
Log.d(TAG, "cachedName: " + callCachedName);
event.put("title", "Called to: " + callCachedName);
}
}
if (typeOfCall == 2){
duration = callCur.getInt(callCur.getColumnIndex(android.provider.CallLog.Calls.DURATION));
Log.d(TAG, "Duration: " + duration);
if (duration == 0){
event.put("description", "Call was interupted");
}
else{
event.put("description", "Duration of call was: " + duration);
}
if (compareNumbers == null){
callName = callCur.getString(callCur.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
Log.d(TAG, "callName: " + callName);
event.put("title", "Call from : " + callName);
}
else {
callCachedName = callCur.getString(callCur.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
Log.d(TAG, "cachedName: " + callCachedName);
event.put("title", "Call from : " + callCachedName);
}
}
if (typeOfCall == 3){
if (compareNumbers == null){
callName = callCur.getString(callCur.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
Log.d(TAG, "callName: " + callName);
event.put("title", "Called to: " + callName);
}
else {
callCachedName = callCur.getString(callCur.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
Log.d(TAG, "cachedName: " + callCachedName);
event.put("title", "Called to: " + callCachedName);
}
if (compareNumbers == null){
callName = callCur.getString(callCur.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
Log.d(TAG, "callName: " + callName);
event.put("title", "Call from : " + callName);
}
else {
callCachedName = callCur.getString(callCur.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
Log.d(TAG, "cachedName: " + callCachedName);
event.put("title", "Call from : " + callCachedName);
}
}
//event.put("eventLocation", "New Yorks");
event.put("dtstart", callDateLong );
Uri eventsUri = Uri.parse("content://com.android.calendar/events");
Uri calUri = getContentResolver().insert(eventsUri, event);
}
}
}
答案 0 :(得分:1)
扩展BroadcastReceiver
并在AndroidManifest.xml
中添加intent-filter
ACTION_ANSWER
条目。然后,您可以使用onReceive
课程的myBroadcastReceiver
方法调用日历更新方法。