Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在没有上下文的情况下从类启动服务_Android_Service_Android Context - Fatal编程技术网

Android 在没有上下文的情况下从类启动服务

Android 在没有上下文的情况下从类启动服务,android,service,android-context,Android,Service,Android Context,我试图弄清楚如何从我创建的单例类启动服务 根据Android文档,startService是一种上下文方法。因此,在我对singleton类进行的方法调用中,我想我应该将应用程序上下文作为参数传递。很简单。然而,当我尝试使用context.startService时,它说startService不是一种上下文方法。那么,我如何从自己的类启动服务呢 API 7 我为什么这么做: 尽管Android SDK提供了一些示例(其中数据库调用直接从UI进行),但Virgil在其2010年的Google I

我试图弄清楚如何从我创建的单例类启动服务

根据Android文档,
startService
是一种上下文方法。因此,在我对singleton类进行的方法调用中,我想我应该将应用程序上下文作为参数传递。很简单。然而,当我尝试使用
context.startService
时,它说
startService
不是一种上下文方法。那么,我如何从自己的类启动服务呢

API 7

我为什么这么做:

尽管Android SDK提供了一些示例(其中数据库调用直接从UI进行),但Virgil在其2010年的Google I/O演示中为REST密集型应用程序提出了一些行为不同的模型。我试图遵循的是(据我所知):

活动调用“服务助手”(singleton),服务助手启动服务,服务查询数据库中的光标,服务还启动调用web服务更新数据库的线程,服务通知服务助手已完成光标查询,服务助手通知活动有光标。

我看到了我的问题:

import sun.org.mozilla.javascript.internal.Context;

startService绝对是一种上下文方法。我正在做一件类似的事情,但不是在一个单例中,而是在一个静态助手方法中,该方法获取作为上下文参数传入的活动上下文。也许你可以提供一点代码摘录来说明你的案例中可能出现的错误我有一种感觉,你会指出什么是错的,我会觉得自己很愚蠢,但这就是。我已经将一个上下文作为参数传递,并且使用该上下文调用了startService,意图作为参数。这完全是胡说八道。这是一种完全不同的语境,这完全是问题所在。谢谢你的评论。