16

我知道,我不是第一个遇到这个问题的onbe,但我尝试了很多解决方案,我发现没有一个有效......也许你可以找到错误

错误(也没有 .class 和 /.Client 取决于其他设置)

12-02 16:40:15.359:W/ActivityManager(74):无法启动服务 Intent { act=com.android.fh.EnOceanApp.Client.class }:未找到

在清单中,这包含在应用程序中,在活动之外(也在活动中和“.Client”中尝试过

onCreate() 中的代码

    startService(new Intent(this, Client.class)); 

或者

 startService(new Intent(this.getApplicationContext(), Client.class));

或者

Intent intent=new Intent("com.android.fh.EnOceanApp.Client.class");
    this.startService(intent);

或者

  Intent intent=new Intent("com.android.fh.EnOceanApp.Client");
    this.startService(intent);

现在,我不再有任何想法了...... com.android.fh.EnOceanApp 是包,Client.java 是这个包中的服务类

和我忘记的清单:

  <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".EnOceanAppActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>      
        </activity>

      <activity android:name=".ListView" 
          android:label="List View">
      </activity> 
      <activity android:name=".GraphView" 
          android:label="Graph View">
      </activity>
      <service 
            android:name=".Client"></service> //with and without ., of course without this comment
    </application>
4

8 回答 8

21

感谢用户njzk2让我注意到发生了什么。

我有同样的问题。如果您之前没有在项目的清单文件中注册,Android OS 似乎无法找到您请求的服务类。

请记住,服务就像一个活动,但没有图形界面。这意味着需要先注册服务才能使用它们

这是您在 Android 项目中注册服务的方式:

<application>
    <!-- your code -->
    <activity>
        <!-- your code  -->
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name="com.your.own.service.class"></service>
</application>

请记住,YourService 类需要从 Service 扩展,否则您的类将不是服务。

public class YourService extends Service{}
于 2012-10-20T00:14:48.877 回答
4

有时您需要在清单中完全限定您的类名,而不是使用缩写形式 (.classname)。当我使用来自不同包的类时,我已经看到了这一点,但也许在这里会有所帮助,因为服务意图可能会超出应用程序。

于 2011-12-02T22:29:26.010 回答
2

所以..只是为了最终帮助别人或不帮助别人:

我创建了一个新项目,复制了源代码并尝试运行它:现在找到了该服务。有什么区别,或者换句话说:我认为什么可能会带来问题:长包名或以 com.android 开头。. . 在新项目中,我刚刚选择了 com.enocean

于 2011-12-04T13:33:13.553 回答
1

尽管这篇文章中的所有答案以及许多相关的Unable to start service Intent: not found Unable to start Service Intent,我仍然很挣扎,我花了一些时间来完成这项工作。我的情况稍微复杂一些,因为我试图在我调用它的不同应用程序中启动一项服务。我想通了,这里是所有细节,以及一些奖励代码。

调用意图的MainActivity(或任何地方)

Intent intent=new Intent("com.example.core.MusicService.1234");
//Or Intent intent=new Intent("com.example.core.MusicService.TOGGLE_PLAYBACK");
PendingIntent pendingIntent = PendingIntent.getService(this, 99, intent, PendingIntent.FLAG_CANCEL_CURRENT);

Manifest: of Service (Service tag inside Application tag) 它是

    <service android:name="com.example.core.MusicService">
        <intent-filter>
            <action android:name="com.example.core.MusicService1234"></action>
        </intent-filter>
        <intent-filter>
            <action android:name="com.example.core.MusicService.TOGGLE_PLAYBACK"></action>
        </intent-filter>
    </service>

音乐服务.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if(intent != null){
        if(intent.getAction() != null){
            if(intent.getAction().contentEquals("com.example.core.MusicService.TOGGLE_PLAYBACK")){
                //Do work here
            }
        }
    }
}

笔记

  • 服务不需要启动,这个意图会启动它
  • “com.example.core.MusicService1234”和“com.example.core.MusicService.TOGGLE_PLAYBACK”可以是任何你想要的,但显然需要将服务清单中的意图过滤器与调用意图相匹配。您可以放置​​其中的多个,以便在服务启动时根据您的意图执行不同的操作
  • 99 可以是任何你想要的,但如果你使用通知,它必须是唯一的
  • 我不确定是否可以在不使用意图过滤器的情况下在不同的应用程序中调用服务(像这样) - 如果是这样,请有人启发我们。我试过了,它不起作用。

归功于:所有帖子的累积信息:)

于 2015-01-17T03:23:36.983 回答
0

这是android的愚蠢错误

这行不通

<service
            android:name=".classname"/>

但这会起作用,有单独的结束标签

<service
            android:name=".classname"></service>
于 2015-02-12T10:29:04.083 回答
0

好吧,就我而言,我必须清理项目。当您在包/项目中为服务创建了一个新的 Java 类但没有在之后构建/清理项目时,有时会发生这种情况。就我而言,我只需要清理项目即可摆脱错误。

于 2015-03-01T06:53:15.603 回答
0

我犯了一个愚蠢的错误,将标签添加到清单中的单独标签中。

在这种情况下,当前应用程序无法找到定义的服务。

希望你跳过这个错误:)

谢谢。

于 2014-03-14T00:24:31.553 回答
0

如果有人看到这一点并且遇到与我相同的问题,那是因为我遵循了指南并使用context.startService()而不是context.startActivity()

于 2016-04-29T14:41:10.333 回答