如何从android上的服务调用主线程函数?

如何从android上的服务调用主线程函数?,android,static,cursor,Android,Static,Cursor,我有一个主类,它包含函数isSyncAllowed。我开始另一项服务。如何从此服务调用函数IsSyncCallowed? 它说: 无法对进行静态引用 非静态法 isSyncAllowed(布尔值)来自类型 主要 如果我将该函数的类型更改为静态并传递上下文,我就会遇到一个问题,即StartMagingCursor不能是静态的 我怎样才能修好它 Upd。以下是同步服务的代码: public class SyncService extends BroadcastReceiver { ... p

我有一个主类,它包含函数
isSyncAllowed
。我开始另一项服务。如何从此服务调用函数
IsSyncCallowed
? 它说:

无法对进行静态引用 非静态法 isSyncAllowed(布尔值)来自类型 主要

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

我怎样才能修好它

Upd。以下是同步服务的代码:

public class SyncService extends BroadcastReceiver {
...
    public void onReceive(Context context, Intent intent) {
    ...
    Boolean isDownloadAllowed = Kinobaza.isSyncAllowed(true);
    }
public class Kinobaza extends ListActivity {
    ...
    public static Boolean isSyncAllowed(Boolean showToasts) {
        ...
        Cursor notesCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(notesCursor); // here is the error
    }
}
这是Main的代码:

public class SyncService extends BroadcastReceiver {
...
    public void onReceive(Context context, Intent intent) {
    ...
    Boolean isDownloadAllowed = Kinobaza.isSyncAllowed(true);
    }
public class Kinobaza extends ListActivity {
    ...
    public static Boolean isSyncAllowed(Boolean showToasts) {
        ...
        Cursor notesCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(notesCursor); // here is the error
    }
}

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

如果您在
Main
的实例方法中,那么只需执行
isSyncAllowed(…)

编辑-现在我看到了您的代码,您可能应该通过您提供给服务的
意图
传递
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);

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

如果没有代码,并且我们必须猜测
Main
是什么,您不可能得到太多帮助。代码很长。那么让我重新表述我的问题。如果我保持isSyncAllowed为非静态,然后创建对象,如
Main Main=new Main()
,那么我的应用程序就会崩溃。Main是应用程序的主要类。