我有一個使用片段的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;
}
u能暫爲getCallDetails共享代碼() – Antony
@Anthony對不起,我也許應該的補充,在開始,而不是告訴你們看一個教程。現在就添加它。 請注意:我實際上並不需要從此代碼的所有數據;只需撥打電話 – BrianMac21
的耐用性,您是否添加了權限android.permission.READ_CONTACTS? – Antony