我在服务中启动了一个IntentService。问题是,当服务激发启动IntentService的意图时,什么都不会发生(意图没有到达IntentService)。
详细信息:服务本身就是类,IntentService被定义为服务中的内部类。原因是共享全局变量(不传递它们)。
关于问题所在:所有服务都是这样定义的:(在应用程序标签中)
<service android:name=".MyService" />
<service android:name=".ConnectionIntentService" />
<service android:name=".ListeningIntentService" />否则,启动IntentService的方式如下:
Intent Start_ConnectionIS = new Intent(this, ConnectionIntentService.class);
startService(Start_ConnectionIS);发布于 2013-12-14 17:35:11
服务本身就是类,IntentService被定义为服务中的内部类
这是行不通的,除非IntentService是一个静态的内部类。
原因是共享全局变量(不传递它们)。
Java中没有全局变量。最接近的模拟是静态数据成员,您不需要使用内部类来“共享”这些成员。
IntentService作为服务的内部类有什么问题吗?
您的清单声称您的所有服务都是独立的公共类,而不是内部类。
如果您选择拥有一个BarIntentService,它是FooService的静态内部类,那么语法将是android:name=".FooService$BarIntentService"。非静态内部类不能用于Service,因为Android不能创建它的实例。
https://stackoverflow.com/questions/20586149
复制相似问题