简体   繁体   English

来自服务的活动中的调用方法

[英]call method in activity from service

I have a service that show me a list of information using toast.我有一个服务,它使用 toast 向我显示信息列表。 some of my used function should be in activity (function to get list of call history ....).我使用的一些功能应该处于活动状态(获取通话记录列表的功能......)。 that activity are used only for my function it had no graphic .the functions can not be static it show me error , i essay to use interface but always error please help me to use those function from my activity.该活动仅用于我的功能,它没有图形。功能不能是静态的,它向我显示错误,我使用界面但总是错误,请帮助我使用我的活动中的这些功能。 NB:- i read all anser about problem like this but it is the same thing nothing change - the service had no relation with the activity.注意:-我阅读了所有关于此类问题的分析,但它是同一件事,没有任何变化-该服务与活动无关。

It looks you are not following a standard design pattern for interacting an activity with the service.看起来您没有遵循用于与服务交互的标准设计模式。 Please re-think the case of activity service interaction.请重新思考活动服务交互的案例。 If you want to reuse the method for getting the call records, put this method in an utils class and pass a context to get the call logs-如果你想重用获取通话记录的方法,把这个方法放在一个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();

} }

You can get context in the service by using the method calls-您可以使用方法调用获取服务中的上下文 -

          getBaseContext();
          getApplicationContext();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM