2012-12-14 33 views
2

我正在Android應用程序中工作,我必須獲取最近撥打的20個電話。無法獲取最近撥打的號碼

這裏是我的活動:

public class Calls extends Activity { 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.calls); 
    TextView dialledCall = (TextView)findViewById(R.id.dialledCall); 
     Cursor managedCursor; 
    try { 

StringBuffer sb = new StringBuffer(); 
      String strOrder = CallLog.Calls.DATE + " DESC"; 
      managedCursor = managedQuery(CallLog.Calls.CONTENT_URI,null, CallLog.Calls.TYPE + " = " + CallLog.Calls.OUTGOING_TYPE,null, strOrder + " LIMIT 0, 6"); 
      managedCursor.moveToFirst(); 
      int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER); 
      int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE); 
      int date = managedCursor.getColumnIndex(CallLog.Calls.DATE); 
      int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION); 
      sb.append("Dialled Calls :"); 

      while (managedCursor.moveToNext()) { 
      String phNumber = managedCursor.getString(number); 
      String callType = managedCursor.getString(type); 
      String callDate = managedCursor.getString(date); 
      Date callDayTime = new Date(Long.valueOf(callDate)); 
      String callDuration = managedCursor.getString(duration); 
      String dir = null; 
      int dircode = Integer.parseInt(callType); 

      switch(dircode) { 
      case CallLog.Calls.OUTGOING_TYPE: 
      dir = "Dialled"; 
      break; 
      } 

      sb.append("\nPhone Number : "+phNumber +" \nCall Type : "+dir+" \nCall Date : "+callDayTime+" \nCall duration in sec : "+callDuration); 
      sb.append("\n----------------------------------");   
      } 
      sb.append("\n=================================="); 
      sb.append("\n\n"); 
      managedCursor.close(); 
      Toast.makeText(this, "Eureka :)", Toast.LENGTH_SHORT).show(); 

      managedCursor = managedQuery(CallLog.Calls.CONTENT_URI,null, CallLog.Calls.TYPE + " = " + CallLog.Calls.INCOMING_TYPE,null, strOrder + " LIMIT 0, 6"); 
      managedCursor.moveToFirst(); 
      int number1 = managedCursor.getColumnIndex(CallLog.Calls.NUMBER); 
      int type1 = managedCursor.getColumnIndex(CallLog.Calls.TYPE); 
      int date1 = managedCursor.getColumnIndex(CallLog.Calls.DATE); 
      int duration1 = managedCursor.getColumnIndex(CallLog.Calls.DURATION); 
      sb.append("Received Calls :"); 
      while (managedCursor.moveToNext()) { 

      String phNumber1 = managedCursor.getString(number1); 
      String callType1 = managedCursor.getString(type1); 
      String callDate1 = managedCursor.getString(date1); 
      Date callDayTime1 = new Date(Long.valueOf(callDate1)); 
      String callDuration1 = managedCursor.getString(duration1); 
      String dir1 = null; 
      int dircode = Integer.parseInt(callType1); 

      switch(dircode) { 
      case CallLog.Calls.INCOMING_TYPE: 
      dir1 = "Received"; 
      break; 
      } 
      sb.append("\nPhone Number : "+phNumber1 +" \nCall Type : "+dir1+" \nCall Date : "+callDayTime1+" \nCall duration in sec : "+callDuration1); 
      sb.append("\n----------------------------------");   
      } 
      sb.append("\n=================================="); 
      sb.append("\n\n"); 
      managedCursor.close(); 
      Toast.makeText(this, "Eureka :) 1", Toast.LENGTH_SHORT).show(); 
     managedCursor.close(); 
} 


catch (NullPointerException e) { 
       // TODO: handle exception 
       Toast.makeText(this, "Sorry Man :(", Toast.LENGTH_SHORT).show(); 
       e.printStackTrace(); 
      } 
     } 
     } 

這裏是相應的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true" > 

<Linearlayout android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:orientation="vertical"> 

     <TextView 
      android:id="@+id/dialledCall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:textSize="13dp" /> 

</Linearlayout> 
</ScrollView> 

輸出:我能夠獲得前5個號碼,但接電話我甚至得到messages

我在做什麼錯?

+0

檢查是否真的有一些撥號撥打您的電話或不。 –

+0

我可以看到您編輯的帖子。那麼現在你得到了什麼結果? –

回答

1

最可能的是你缺少權限:

<uses-permission android:name="android.permission.READ_CONTACTS" /> 

或者你的循環是不正確的,嘗試這種循環,而不是:

int count = 0; 
if (cursor.moveToFirst()) { 
    do { 
    // Read your row here 

    ++count; 
    } while (cursor.moveToNext() && count<20); 
} 
cursor.close(); 

這將是更有效也限制數量的行直接在查詢中而不是在循環本身中:

managedQuery(CallLog.Calls.CONTENT_URI, null, 
     CallLog.Calls.TYPE + " = " + CallLog.Calls.OUTGOING_TYPE, 
     null, strOrder + " LIMIT 0, 20"); 
+0

明智的答案與正確的代碼,但對於來電我也接收消息這對我來說是安靜荒謬的,我已經更新了我的編輯,編輯我的代碼,可以看看我要去哪裏錯了 – user1645434