【发布时间】:2014-05-01 18:06:40
【问题描述】:
我有一个使用片段的 Android 应用。我也在按照教程 (CallLogs Tutorial) 来获取通话记录。
我有一个由链接中的教程制作的类,但现在我想从这个类中获取数据并在我的一个片段中使用它。
我尝试过类似以下的方法;
int callsMade = new GetUsage().getCallDetails();
String callsmade = String.valueOf(callsMade);
Log.e("Calls Made:", "Texts: " + callsmade);
但我一直在“Cursor managedCursor”行收到错误(注意:我已将 managedCursor 更改为 getContentResolver,因为 managedCursor 已被弃用并且也出现错误)
我在这里做错了吗?你能想到我能改变的事情吗?
非常感谢您提供的任何帮助。
编辑: 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;
}
【问题讨论】:
-
能否分享一下getCallDetails()的代码
-
@Anthony 对不起,我可能应该在开始时添加,而不是告诉你看教程。现在刚刚添加。请注意:我实际上并不想要此代码中的所有数据;只是通话的持续时间
-
你添加了权限android.permission.READ_CONTACTS ??
-
我做到了。我可以运行活动本身没问题,但是当我尝试将其调用到片段中时,应用程序崩溃
-
GetUsage 是你的活动吗??
标签: android class methods android-fragments