2014-03-24 28 views
1

我有一個使用片段的Android應用程序。我也在按照教程(CallLogs Tutorial)獲取通話記錄。從另一個類獲取方法的片段

我有一個由該教程在鏈接中創建的類,但現在我想從該類獲取數據並將其用在我的一個片段中。

我試過類似下面的東西;

int callsMade = new GetUsage().getCallDetails(); 
    String callsmade = String.valueOf(callsMade); 

    Log.e("Calls Made:", "Texts: " + callsmade); 

但我一直在「光標managedCursor」行收到的錯誤(注:因爲managedCursor我已經改變了managedCursor到getContentResolver已過時,太錯誤)

有什麼我」在這裏做錯了嗎?你可以想到,我可以改變嗎?

非常感謝您提供的幫助。編碼: getCallDetails()的代碼; 。

public int getCallDetails() 
{ 

    StringBuffer sb = new StringBuffer(); 
    Cursor callsCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,null, null,null, CallLog.Calls.DATE + " DESC"); 
    //Cursor planCursor=PlansHelper.query("plans", null, null, null, null); 
    int name = callsCursor.getColumnIndex(CallLog.Calls.CACHED_NAME); 
    int number = callsCursor.getColumnIndex(CallLog.Calls.NUMBER); 
    int type = callsCursor.getColumnIndex(CallLog.Calls.TYPE); 
    int date = callsCursor.getColumnIndex(CallLog.Calls.DATE); 
    int duration = callsCursor.getColumnIndex(CallLog.Calls.DURATION); 
    int allduration = 0; 


    sb.append("Call Details :"); 
    while (callsCursor.moveToNext()) 
    { 

     String phName = callsCursor.getString(name); 
     String phNumber = callsCursor.getString(number); 

     String callType = callsCursor.getString(type); 
     int callTypeCode = Integer.parseInt(callType); 

     String callDate = callsCursor.getString(date); 
     Date formatDate = new Date(Long.valueOf(callDate)); 
     SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
     String dateStr = sdf.format(formatDate); 

     String userdate = "18/03/2014"; 

     //Date parseddate = sdf.parse(userdate1); 

     Calendar c = Calendar.getInstance(); 
     try { 
      c.setTime(sdf.parse(userdate)); 
     } catch (ParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     c.add(Calendar.DATE, -5); // number of days to add 
     userdate = sdf.format(c.getTime()); // dt is now the new date 


     String callDuration = callsCursor.getString(duration); 
     float durationToFloat =Float.parseFloat(callDuration); 
     float durationToMins = durationToFloat/60; 
     int durationRounded = Math.round(durationToMins); 
     int callDurationMin = durationRounded; 


     if(callTypeCode==CallLog.Calls.OUTGOING_TYPE) 
     { 

      //if(dateStr.equals(userdate) || dateStr.equals(userdate1)) 
      if(formatDate.after(c.getTime())) 
      { 
       sb.append("\nName:--- " + phName + "\nPhone Number:--- " + phNumber + " \nCall Date:--- " + dateStr 
         + " \nCall duration in min :--- " + callDurationMin); 
       sb.append("\n----------------------------------"); 
       allduration = allduration + callDurationMin; 
       Log.d(getClass().getSimpleName(), "Done"); 
      } 
     } 


    } 

    sb.append("##" + allduration + "##"); 
    getCalls.setText(sb.toString()); 
    callsCursor.close(); 

    return allduration; 

} 
+0

u能暫爲getCallDetails共享代碼() – Antony

+0

@Anthony對不起,我也許應該的補充,在開始,而不是告訴你們看一個教程。現在就添加它。 請注意:我實際上並不需要從此代碼的所有數據;只需撥打電話 – BrianMac21

+0

的耐用性,您是否添加了權限android.permission.READ_CONTACTS? – Antony

回答

0

改變線INT callsMade =新GetUsage()getCallDetails();下面的代碼

Activity activity = getActivity(); 
if(activity instanceof GetUsage) 
    { 
    GetUsage myactivity = (GetUsage) activity; 
    myactivity.getCallDetails(); 
    } 
+1

非常感謝爲了幫助我,我非常感謝。 我試着你的建議和if語句不運行(我把一個日誌消息在他們的嘗試)。這條線試圖做什麼,或者你知道爲什麼會發生這種情況? – BrianMac21

+0

在哪個活動中你正在膨脹這個片段 - Wats你的片段的praent活動? – Antony

+0

我的主動活動。它所做的所有工作都會擴展片段並控制片段的選項卡 – BrianMac21

相關問題