获取服务调用的线程中的上下文

发布于 2024-11-28 12:16:32 字数 665 浏览 1 评论 0原文

我有以下代码:

public class DumpLocationLog extends Thread {
    LocationManager lm;
    LocationHelper loc;
    public void onCreate() {
        loc = new LocationHelper();
        lm = (LocationManager) Context.getSystemService(Context.LOCATION_SERVICE);
    }
    public void run() {
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, loc);
    }
}

我希望它从远程服务运行,但在 lm = (LocationManager) Context.getSystemService(Context.LOCATION_SERVICE); 行中我得到一个 NullPointerException 当然是错误,因为 context 为空。

我怎样才能得到这里的上下文? getBaseContext()getApplicationContext() 不起作用。

I have the following piece of code:

public class DumpLocationLog extends Thread {
    LocationManager lm;
    LocationHelper loc;
    public void onCreate() {
        loc = new LocationHelper();
        lm = (LocationManager) Context.getSystemService(Context.LOCATION_SERVICE);
    }
    public void run() {
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, loc);
    }
}

I want it to be run from a remote service but in the line lm = (LocationManager) Context.getSystemService(Context.LOCATION_SERVICE); I get a NullPointerException error of course, because the context is null.

How can I get the context here? getBaseContext() or getApplicationContext() does not work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

池木 2024-12-05 12:16:33

默认情况下,Thread 无法直接访问Context;线程也不需要 onCreate 方法(我猜你是手动调用的) - 我只需将其更改为构造函数。您只需在线程的构造函数中传入 Context:

public class DumpLocationLog extends Thread {
    LocationManager lm;
    LocationHelper loc;
    public DumpLocationLog(Context context) {
        loc = new LocationHelper();
        lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }
    public void run() {
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, loc);
    }
}

如果从 Service 内部使用(Service 是一个Context 的子类,所以 this 在这里起作用)。您可以通过调用 start() 方法来启动线程。

A Thread would not have any direct access to a Context by default; you would have to pass it in. A Thread also does not need an onCreate method (which I guess you are calling manually) - I would just change it to a constructor. You can just pass in a Context in the constructor of the Thread:

public class DumpLocationLog extends Thread {
    LocationManager lm;
    LocationHelper loc;
    public DumpLocationLog(Context context) {
        loc = new LocationHelper();
        lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }
    public void run() {
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, loc);
    }
}

You would instantiate it with new DumpLocationLog(this); if used from inside a Service (a Service is a subclass of Context, so this works here). You start the thread by calling the start() method.

情仇皆在手 2024-12-05 12:16:33

您可以通过构造函数传递有效的上下文,或者,如果您的类是服务内的内部类,您可以使用 ServiceClassName.this.getContext()

You could pass a valid context through the constructor, or, if your class is an inner class inside a service you could use ServiceClassName.this.getContext()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文