我有一个使用片段的Android应用。我也正在按照教程(CallLogs Tutorial)来获取通话记录。
我在链接中有一个由教程制作的类,但现在我想从这个类中获取数据并在我的一个片段中使用它。
我尝试过以下内容;
int callsMade = new GetUsage().getCallDetails();
String callsmade = String.valueOf(callsMade);
Log.e("Calls Made:", "Texts: " + callsmade);
但是我一直在“Cursor managedCursor”行上出错(注意:我已经将managedCursor更改为getContentResolver,因为不推荐使用managedCursor并且也得到了错误)
我在这里做错了吗? Anthing你能想到我可以改变吗?
非常感谢您提供的任何帮助。
编辑: 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 :(得分:0)
将行 int callsMade = new GetUsage()。getCallDetails(); 更改为以下代码
Activity activity = getActivity();
if(activity instanceof GetUsage)
{
GetUsage myactivity = (GetUsage) activity;
myactivity.getCallDetails();
}