[英]How to call main thread function from the Service on android?
I have Main class and it contains function isSyncAllowed
. 我有Main类,它包含函数
isSyncAllowed
。 I start another service. 我开始另一项服务。 How to call function
isSyncAllowed
from this service? 如何从该服务调用
isSyncAllowed
函数? It says: 它说:
Cannot make a static reference to the non-static method isSyncAllowed(Boolean) from the type Main
无法从类型Main中静态引用非静态方法isSyncAllowed(Boolean)
If I change type of that function to static
and pass context
, I come to the problem that startManagingCursor can not be static. 如果我将该函数的类型更改为
static
并传递context
,则会出现startManagingCursor不能为静态的问题。
How can I fix it? 我该如何解决?
Upd. 更新。 Here is the code of SyncService :
这是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 : 这是Main的代码:
public class Kinobaza extends ListActivity {
...
public static Boolean isSyncAllowed(Boolean showToasts) {
...
Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor); // here is the error
}
}
You are probably doing Main.isSyncAllowed(...)
instead of main.isSyncAllowed(...)
, where main
is an object of class Main
. 您可能正在执行
Main.isSyncAllowed(...)
而不是main.isSyncAllowed(...)
,其中main
是Main
类的对象。
If you are inside an instance method of Main
, then you can simply do isSyncAllowed(...)
. 如果您位于
Main
的实例方法中,则只需执行isSyncAllowed(...)
。
Edit - now that I see your code, you should probably pass isSyncAllowed
via the Intent
you give to the service. 编辑 -现在我看到了您的代码,您可能应该通过提供给服务的
Intent
传递isSyncAllowed
。 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. 您的服务不需要在活动中调用实例方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.