从服务调用 activity 中的方法
call method in activity from service
我有一项服务可以使用 toast 向我显示信息列表。
我使用的一些功能应该在 activity 中(获取通话记录列表的功能....)。 activity 仅用于我的函数,它没有图形。函数不能是静态的,它向我显示错误,我尝试使用界面但总是错误
请帮助我使用 activity 中的那些功能。
注意:- 我阅读了所有关于此类问题的答案,但这是一回事,没有任何改变
- 该服务与 activity.
无关
看来您没有遵循 activity 与服务交互的标准设计模式。
请重新考虑 activity 服务交互的情况。
如果你想重用获取通话记录的方法,把这个方法放在一个utils class 并传递一个上下文来获取通话记录-
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();
我有一项服务可以使用 toast 向我显示信息列表。 我使用的一些功能应该在 activity 中(获取通话记录列表的功能....)。 activity 仅用于我的函数,它没有图形。函数不能是静态的,它向我显示错误,我尝试使用界面但总是错误 请帮助我使用 activity 中的那些功能。 注意:- 我阅读了所有关于此类问题的答案,但这是一回事,没有任何改变 - 该服务与 activity.
无关看来您没有遵循 activity 与服务交互的标准设计模式。 请重新考虑 activity 服务交互的情况。 如果你想重用获取通话记录的方法,把这个方法放在一个utils class 并传递一个上下文来获取通话记录-
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();