【问题标题】:Can not pass the context parameter of a JobService to a class无法将 JobService 的上下文参数传递给类
【发布时间】:2018-08-07 13:35:08
【问题描述】:

我无法将 JobService 的上下文传递给位置提供程序类。这是我尝试过的:

我的位置提供者类:

public class Mylocation implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
LocationListener {

    private Context context;

    public void Mylocation(Context context){
        this.context=context;
    }

    //other methods of Location provider
}

这是我的 JobService:

public class LocationMonitoringService extends JobService  {
    Mylocation mylocation=new Mylocation(this);
    //blah blah
    }

当我写this 时,Mylocation(this) 出现错误。实际上我想将 JobService 的上下文传递给类。这是我在 Android Studio 中遇到的错误:

Mylocation中的Mylocation()不能应用于 com.example.app.JobService

据我所知,JobService 扩展 service 并且服务是上下文本身。

【问题讨论】:

    标签: java android android-context


    【解决方案1】:

    从构造函数中移除void

    你的构造函数应该是这样的

    public Mylocation(Context context){
            this.context=context;
        }
    

    【讨论】:

    • 据我所知 JobService 扩展 serviceservicecontext 其本身。
    • 试试这个,它绝对会帮助你@AliSheikhpour
    • 我试过了,我得到了同样的错误。我的类结构定义应该有问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-11
    • 2020-05-15
    • 2015-04-18
    • 1970-01-01
    相关资源
    最近更新 更多