无法启动服务意图

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)

  • 这听起来像是一个增强的android lint工作,可以解决这些简单的错误!我们最终会得到什么. (7认同)

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)

  • @Vinay:"我需要从不同的应用程序访问相同的内容."你仍然不需要`android:process =":remote"`,所以请删除它,因为它所做的就是占用额外的内存,效益.远程服务是通过AIDL提供API并且与`android:process`属性无关的服务.这是一个示例远程服务:http://github.com/commonsguy/cw-advandroid/tree/master/AdvServices/RemoteService/,这里是该服务的相应客户端:http://github.com/commonsguy/cw -advandroid /树/主/ AdvServices/RemoteClient / (2认同)

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)

  • 老兄,非常感谢你的回答.我一直在努力解决一个friggin问题,事实证明我的服务是在一个不同的包中.如果我能拥抱你,我愿意! (3认同)

小智 5

我希望我也可以帮助这些信息的人:我将我的服务类移动到另一个包中,并修复了引用.该项目非常好,但活动无法找到服务类.

通过观察logcat中的日志,我注意到有关该问题的警告:活动无法找到服务类,但有趣的是该包是不正确的,它包含一个"/"字符.编译器正在寻找

com.something./service.MyService

代替

com.something.service.MyService

我将服务类从包中移出并重新进入,一切正常.

  • 我不得不说它对我也有用.奇怪的问题,我花了半个小时才找到我们的评论.谢谢! (2认同)