2011-04-12 86 views
0

的自動更新我的Android初學寫developer.I一些簡單的應用程序,女巫增加約calls.Now我想添加自動更新,如果有人給我打電話,它全自動更新日曆的日曆信息。安卓:我的應用程序

的源代碼:

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); 

     } 
    } 
} 

回答

1

擴展BroadcastReceiver並在AndroidManifest.xmlACTION_ANSWERintent-filter添加一個條目。然後,您可以從您的myBroadcastReceiver類的onReceive方法調用你的壓延機更新方法。