如何从Android上的Service调用主线程函数?

发布于 2024-10-29 23:02:03 字数 838 浏览 1 评论 0原文

我有 Main 类,它包含函数 isSyncAllowed 。我启动另一个服务。如何从此服务调用函数isSyncAllowed? 它说:

无法静态引用 非静态方法 isSyncAllowed(Boolean) 来自类型 主要

如果我将该函数的类型更改为静态并传递上下文,我会遇到 startManagingCursor 不能是静态的问题。

我该如何修复它?

更新。这是SyncService的代码:

public class SyncService extends BroadcastReceiver {
...
    public void onReceive(Context context, Intent intent) {
    ...
    Boolean isDownloadAllowed = Kinobaza.isSyncAllowed(true);
    }

这是Main的代码:

public class Kinobaza extends ListActivity {
    ...
    public static Boolean isSyncAllowed(Boolean showToasts) {
        ...
        Cursor notesCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(notesCursor); // here is the error
    }
}

I have Main class and it contains function isSyncAllowed. I start another service. How to call function isSyncAllowed from this service?
It says:

Cannot make a static reference to the
non-static method
isSyncAllowed(Boolean) from the type
Main

If I change type of that function to static and pass context, I come to the problem that startManagingCursor can not be static.

How can I fix it?

Upd. Here is the code of SyncService:

public class SyncService extends BroadcastReceiver {
...
    public void onReceive(Context context, Intent intent) {
    ...
    Boolean isDownloadAllowed = Kinobaza.isSyncAllowed(true);
    }

and here is the code of Main:

public class Kinobaza extends ListActivity {
    ...
    public static Boolean isSyncAllowed(Boolean showToasts) {
        ...
        Cursor notesCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(notesCursor); // here is the error
    }
}

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

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

发布评论

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

评论(1

垂暮老矣 2024-11-05 23:02:03

您可能正在执行 Main.isSyncAllowed(...) 而不是 main.isSyncAllowed(...),其中 main 是一个对象类Main

如果您位于 Main 的实例方法内,则只需执行 isSyncAllowed(...) 即可。

编辑 - 现在我看到了您的代码,您可能应该通过您提供给服务的 Intent 传递 isSyncAllowed 。当您启动服务时:

Intent intent = /* however you were constructing your intent */
boolean syncAllowed = /* calculate syncAllowed by calling your existing method */
intent.putExtra("syncAllowed", syncAllowed);
    ...

然后在您的服务中您可以检索它:

boolean syncAllowed = getIntent().getBooleanExtra("syncAllowed", true);

您的服务不需要在您的活动上调用实例方法。

You are probably doing Main.isSyncAllowed(...) instead of main.isSyncAllowed(...), where main is an object of class Main.

If you are inside an instance method of Main, then you can simply do isSyncAllowed(...).

Edit - now that I see your code, you should probably pass isSyncAllowed via the Intent you give to the service. When you start the service:

Intent intent = /* however you were constructing your intent */
boolean syncAllowed = /* calculate syncAllowed by calling your existing method */
intent.putExtra("syncAllowed", syncAllowed);
    ...

and then in your service you can retrieve it:

boolean syncAllowed = getIntent().getBooleanExtra("syncAllowed", true);

Your service shouldn't need to call instance methods on your activities.

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