首页 文章

无法启动服务意图

提问于
浏览
80

我有一个服务类 . 我已将此类导出到jar,并且已将jar嵌入到我的客户端应用程序中 .

需要时,我会调用服务类 . 当我尝试这样做时,我收到以下错误:

Unable to start service Intent {comp={com.sample.service/com.sample.service.serviceClass}} : not found

我有其他类,除了服务类,我能够访问(创建该类的对象),它们在同一个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   
}

客户端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>

提前致谢,
维奈

6 回答

  • 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);
    }
    
  • 0

    对于遇到这个帖子的其他人,我遇到了这个问题,并且正在拉我的头发 . I had the service declaration OUTSIDE of the '< application>' end tag DUH!

    RIGHT:

    <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="..." />
    

    WRONG but still compiles without errors:

    <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="..." />
    
  • 5

    1)检查清单中的服务声明是否嵌套在应用程序标记中

    <application>
        <service android:name="" />
    </application>
    

    2)检查 service.java 与活动是否在同一个包或差异包中

    <application>
        <!-- service.java exists in diff package -->
        <service android:name="com.package.helper.service" /> 
    </application>
    
    <application>
        <!-- service.java exists in same package -->
        <service android:name=".service" /> 
    </application>
    
  • 0

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

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

    com.something./service.MyService

    代替

    com.something.service.MyService

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

  • 72

    就我而言,Intent的数据传输最大上限为1 MB . 我只会使用缓存或存储 .

  • 32

    我发现了同样的问题 . 我失去了差不多一天试图从 OnClickListener 方法开始服务 - 在 onCreate 之外,在1天之后,我仍然失败了!!!!很沮丧!我正在查看示例示例 RemoteServiceController . 他们的工作,但我的实施不起作用!

    对我有用的唯一方法是从 onCreate 方法内部 . 没有其他变种工作,相信我,我已经尝试过所有 .

    结论:

    • 如果您将服务类放在与mainActivity不同的包中,我将会遇到各种错误

    • 另外一个"/"无法找到服务的路径,尝试从 Intent(package,className) 开始尝试,没有其他类型的Intent启动

    • 我将服务类移动到活动的最终表单的相同包中

    • 希望这有助于某人通过在 onCreate 方法中定义listerners onClick ,如下所示:

    public void onCreate() {
    //some code......
        Button btnStartSrv  = (Button)findViewById(R.id.btnStartService);
        Button btnStopSrv  = (Button)findViewById(R.id.btnStopService);
    
        btnStartSrv.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                startService(new Intent("RM_SRV_AIDL"));
            }
        });
    
        btnStopSrv.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                stopService(new Intent("RM_SRV_AIDL"));
            }
        });
    
    } // end onCreate
    

    对于Manifest文件也非常重要,请确保该服务是应用程序的子代:

    <application ... >
        <activity ... >
         ...
        </activity>
        <service
            android:name="com.mainActivity.MyRemoteGPSService"
            android:label="GPSService"
            android:process=":remote">
    
            <intent-filter>
                 <action android:name="RM_SRV_AIDL" />
            </intent-filter>
        </service>
    </application>
    

相关问题