我有一个服务,可以使用 toast 向我显示信息列表。我使用的一些功能应该在活动中(获取通话记录列表的功能......该活动仅用于我的函数它没有图形.函数不能是静态的它向我显示错误,我论文使用界面但总是错误请帮助我从我的活动中使用这些功能。注意:- 我阅读了所有关于此类问题的内容,但这是同样的事情,没有任何变化- 该服务与活动无关。
看起来您没有遵循将活动与服务交互的标准设计模式。请重新思考活动服务交互的情况。如果要重用该方法获取呼叫记录,请将此方法放在 utils 类中并传递上下文以获取呼叫日志-
private static String getCallDetails(Context context) {
StringBuffer stringBuffer = new StringBuffer();
Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI,
null, null, null, CallLog.Calls.DATE + " DESC");
int number = cursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = cursor.getColumnIndex(CallLog.Calls.TYPE);
int date = cursor.getColumnIndex(CallLog.Calls.DATE);
int duration = cursor.getColumnIndex(CallLog.Calls.DURATION);
while (cursor.moveToNext()) {
String phNumber = cursor.getString(number);
String callType = cursor.getString(type);
String callDate = cursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = cursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
stringBuffer.append("nPhone Number:--- " + phNumber + " nCall Type:--- "
+ dir + " nCall Date:--- " + callDayTime
+ " nCall duration in sec :--- " + callDuration);
stringBuffer.append("n----------------------------------");
}
cursor.close();
return stringBuffer.toString();
}
您可以使用方法调用在服务中获取上下文-
getBaseContext();
getApplicationContext();