繁体   English   中英

Android:无法启动服务意图:找不到?

[英]Android: unable to start service intent: not found?

我知道,我不是第一个遇到这个问题的人,但我尝试了很多解决方案,我发现并且没有人工作......也许你会发现错误

错误(也是如此,没有.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>

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

我遇到了同样的问题。 如果您之前没有在proyect的清单文件中注册过,那么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{}

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

那么..只是为了最终帮助别人:

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

尽管这篇文章中的所有答案都有很多相关的无法启动服务意图:未找到 无法启动服务意图 ,我仍然很挣扎,我花了一些时间来实现这一目标。 我的场景稍微复杂一点,因为我正在尝试在一个不同的应用程序中启动一个我正在调用它的应用程序。 我想出来了,这里有所有细节,还有一些红利代码。

调用的意图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: Service(Application标签内的服务标签)它是

    <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>

MusicService.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”可以是您想要的任何内容,但显然需要将服务清单中的intent-filter与调用intent匹配。 您可以放置​​其中的多个,这样您可以在服务启动时执行不同的操作,具体取决于您的意图值
  • 99可以是您想要的任何内容,但如果您使用通知则必须是唯一的
  • 我不确定是否可以在不使用意图过滤器的情况下在不同的应用程序中调用服务(如此) - 如果是,请有人指导我们。 我试过但它不起作用。

信用:来自所有帖子的累积信息:)

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

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

希望你跳过那个错误:)

谢谢。

这是android的愚蠢错误

这不行

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

但这将有效,有单独的结束标记

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

好吧,在我的情况下,我不得不清理项目。 有时会在您的软件包/项目中为服务创建一个新的Java类但在后面没有构建/清理项目时发生。 在我的情况下,我只需要清理项目以摆脱错误。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM