Vin*_*ay 80 service android intentfilter android-intent
我有一个服务类.我已将此类导出到jar,并且已将jar嵌入到我的客户端应用程序中.
需要时,我会调用服务类.当我尝试这样做时,我收到以下错误:
Unable to start service Intent {comp={com.sample.service/com.sample.service.serviceClass}} : not found
Run Code Online (Sandbox Code Playgroud)
除了服务类之外,我还有其他类,我可以访问它(创建该类的对象),它们位于同一个jar中.
我觉得我错过了我的配置或清单中的一些东西.
请帮我识别一下.我的代码如下:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent () ;
intent.setClassName("com.sample.service" ,"com.sample.service.serviceClass") ;
this.startService(intent) ; // when I call this line I get the message...
// binding other process continue here
}
Run Code Online (Sandbox Code Playgroud)
客户端manifest.xml
<service android:name="com.sample.service.serviceClass"
android:exported="true" android:label="@string/app_name"
android:process=":remote">
<intent-filter><action android:name="com.sample.service.serviceClass"></action>
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
提前谢谢,
Vinay
Blu*_*ell 72
对于遇到这个帖子的其他人,我遇到了这个问题并且正在拉我的头发. 我有'<application>'结束标签DUH的服务声明OUTSIDE!
对:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...>
...
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity ...>
...
</activity>
<service android:name=".Service"/>
<receiver android:name=".Receiver">
<intent-filter>
...
</intent-filter>
</receiver>
</application>
<uses-permission android:name="..." />
Run Code Online (Sandbox Code Playgroud)
错误但仍然编译没有错误:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...>
...
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity ...>
...
</activity>
</application>
<service android:name=".Service"/>
<receiver android:name=".Receiver">
<intent-filter>
...
</intent-filter>
</receiver>
<uses-permission android:name="..." />
Run Code Online (Sandbox Code Playgroud)
Com*_*are 48
首先,你不需要android:process=":remote"
,所以请删除它,因为它所做的只是占用额外的RAM而没有任何好处.
其次,由于<service>
元素包含一个动作字符串,请使用它:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=new Intent("com.sample.service.serviceClass");
this.startService(intent);
}
Run Code Online (Sandbox Code Playgroud)
Jia*_*ong 32
1)检查清单中的服务声明是否嵌套在应用程序标记中
<application>
<service android:name="" />
</application>
Run Code Online (Sandbox Code Playgroud)
2)检查您service.java
是否与活动在同一个包或差异包中
<application>
<!-- service.java exists in diff package -->
<service android:name="com.package.helper.service" />
</application>
Run Code Online (Sandbox Code Playgroud)
<application>
<!-- service.java exists in same package -->
<service android:name=".service" />
</application>
Run Code Online (Sandbox Code Playgroud)
小智 5
我希望我也可以帮助这些信息的人:我将我的服务类移动到另一个包中,并修复了引用.该项目非常好,但活动无法找到服务类.
通过观察logcat中的日志,我注意到有关该问题的警告:活动无法找到服务类,但有趣的是该包是不正确的,它包含一个"/"字符.编译器正在寻找
com.something./service.MyService
代替
com.something.service.MyService
我将服务类从包中移出并重新进入,一切正常.