无法启动服务意图:未找到

40

我遇到了和这里描述的同样问题,但我不知道出了什么问题。

我的问题是:无法启动服务Intent { act=.connections.MoodyService } U=0:找不到

编辑 我已经在源代码中将包名从connections改为service,抱歉造成混淆

我的manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.moody"
android:installLocation="auto"
android:versionCode="0"
android:versionName="0.6.7 alpha" >

<uses-sdk
    android:maxSdkVersion="18"
    android:minSdkVersion="14"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:allowClearUserData="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="activities.MainActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="activities.Menu_esq"
        android:label="@string/title_activity_menu_esq" >
    </activity>
    <activity
        android:name="activities.BaseActivity"
        android:label="@string/title_activity_base" >
    </activity>
    <activity
        android:name="activities.MainView"
        android:label="@string/title_activity_main_view" >
    </activity>
    <activity
        android:name="activities.LoginActivity"
        android:label="@string/app_name"
        android:noHistory="true"
        android:windowSoftInputMode="adjustResize|stateVisible" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.moody.LeftActivity"
        android:label="@string/title_activity_left" >
    </activity>
    <activity
        android:name="com.example.moody.RightActivity"
        android:label="@string/title_activity_right" >
    </activity>
    <activity
        android:name="activities.UserDetailsActivity"
        android:label="@string/title_activity_user_details" >
    </activity>
    <activity
        android:name="fragments.TopicsPreview"
        android:label="@string/title_activity_copy_of_topics_preview" >
    </activity>
    <activity android:name="activities.Loading" >
    </activity>

    <service
        android:name=".service.MoodyService"
        android:icon="@drawable/ic_launcher"
        android:label="@string/moody_service" >
    </service>
</application>

服务是一种包装,MoodyService是类名。

我的服务类。

public class MoodyService extends Service {

public MoodyService() {
    // TODO Auto-generated constructor stub
}

private boolean isRunning = false;
Object getContent;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub

    return null;
}

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    // Announcement about starting
    Toast.makeText(this, "Starting the Demo Service", Toast.LENGTH_SHORT)
            .show();

    // Start a Background thread
    isRunning = true;
    Thread backgroundThread = new Thread(new BackgroundThread());
    backgroundThread.start();

    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();

    // Stop the Background thread
    isRunning = false;

    // Announcement about stopping
    Toast.makeText(this, "Stopping the Demo Service", Toast.LENGTH_SHORT)
            .show();
}

private class BackgroundThread implements Runnable {
    int counter = 0;

    public void run() {
        try {
            counter = 0;
            while (isRunning) {
                System.out.println("" + counter++);
                new Contents().getAll(getResources(),
                        getApplicationContext());
                Thread.currentThread().sleep(5000);
            }

            System.out.println("Background Thread is finished.........");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

而且在我的主要意图中。

Intent start = new Intent(".service.MoodyService");
        this.startService(start);

并尝试了

Intent intent = new Intent(this, MoodyService.class);
        this.startService(intent);

并尝试使用完整路径

<service
    android:name="com.example.moody.service.MoodyService"
    android:icon="@drawable/ic_launcher"
    android:label="@string/moody_service" >

仍然有一些错误,请问在'act=com.example.moody.connections.MoodyService'的包中出现了错误,但是您的包名却是"com.example.moody.service.MoodyService",请问这是为什么呢? - Shakeeb Ayaz
@ShakeebS 我换了一个新的包,忘记更新帖子了。 - firetrap
我发现了这篇博客文章,非常好地解释了compileSdk 30问题。 https://asvid.github.io/2021-09-03-android-service-binding-on-api30 - MikeMcC
8个回答

30

已解决

我在清单文件中删除了包名开头的句点,这样就可以工作了,换句话说:

以下方式不可行:

.yourPackage.YourClass

但是这个确实有效:

 yourPackage.YourClass

并且在主要部分:

Intent intent = new Intent(this, MoodyService.class);
        this.startService(intent);

但这与文档中所写的相矛盾:

android:name 实现服务的Service子类的名称。这应该是一个完全限定的类名(例如,"com.example.project.RoomService")。 但是,作为一种缩写方式,如果名称的第一个字符是句点(例如,".RoomService"),则它将附加到 在元素中指定的包名后面。发布应用程序后,不应更改此名称(除非您已将android:exported ="false")。

没有默认值。必须指定名称。


1
完全错过了,尽管这是你的错误。当你在清单文件的根目录中写入package="com.example.moody",并且你写android:name=".service.MoodyService"时,系统将会寻找一个名为"com.example.moody.service.MoodyService"的服务。但是你在q的顶部输出的是"com.example.moody.connections.MoodyService",因此我认为这是某个时候的复制粘贴错误。 - cYrixmorten
1
仍然存在一些错误,为什么您的错误在包“act=com.example.moody.connections.MoodyService”中,但是您的包名称为“com.example.moody.service.MoodyService”? - Shakeeb Ayaz
这是因为我已经更新了代码中的包并编辑了问题,但忘记在问题中更新包名称。 - firetrap
当我重命名我的Service类时,我也遇到了同样的问题。不得不在清单文件中手动更改它。 - fersarr
在我的情况下,这个问题是反过来的。我将清单中的“yourPackage.YourClass”更改为“.yourPackage.YourClass”,然后它就可以工作了。我意识到“.”是一个非常重要的东西 :) @firetrap 感谢您的建议 :) - Yog Guru
显示剩余2条评论

23

这项服务也必须被包含在清单中:

<service android:name="com.x.x.serviceclass"></service>

1
如果这个文件在androidTest目录中呢?那里需要有一个单独的清单文件吗? - IgorGanapolsky
谢谢。我忘记声明服务,所以遇到了这个问题。 - Ambar Jain

10

请确保已在清单文件中声明了您的服务。

<service  
      android:name=".MyService"  
      android:enabled="true" >
</service>

尝试使用getApplicationContext()代替"this"关键字。

startService(new Intent(getApplicationContext(), MoodyService.class));

“enabled” 是用来做什么的? - IgorGanapolsky

4

我不知道你为什么使用类似包名的服务名,但为什么不使用类名来启动服务呢?

Intent intent = new Intent(context, YourService.class);
context.startService(intent);

我尝试了,没有任何区别,还是出现同样的错误。我所做的是:“Intent intent = new Intent(this, MoodyService.class); this.startService(intent);”,并且也更改为一个新的包,但结果相同。这只是因为那个包不再使用了。 - firetrap
这正是我遇到的确切问题。AndroidManifest.xml没问题,但我在BroadcastReceiver中传递了onReceive()接收到的意图。 - GrandAdmiral

3

我认为服务的清单包名是错误的,因为你说你的包名是connections,所以它应该像这样:

  android:name ="connections.MoodyService"

或者

  android:name="com.example.moody.connections.MoodyService"

要调用服务,请执行以下操作

  Intent intent = new Intent(this, MoodyService.class);
    this.startService(intent);

是的,你说得对,就像我在我的答案中提到的那样,但文档上说:“然而,作为一种简写方式,如果名称的第一个字符是句点(例如“.RoomService”),它将附加到元素中指定的包名称”,所以我使用了句点,因为service是包名。 - firetrap
connection 是包名或 service 是包名。 - Shakeeb Ayaz
我已经更新了问题,MoodyService.java原来在“connections”包中,现在在“service”包中。我的错误不是包名,而是我忘记更新问题了。 - firetrap

1

我的服务在“service”包中,我的清单服务条目如下:

     <service
        android:name="service.TimerService"
        android:exported="false" >
    </service>

0

你在服务中创建了一个空构造函数吗?

如果没有,请尝试创建一个。

同时不确定您是否可以像那样使用Intent。您可以尝试使用“new Intent(this,MoodyService.class)”语法。


是的,我试过了,还尝试了以下代码: Intent intent = new Intent(this, MoodyService.class); this.startService(intent); 我已经更新了我的问题,并附上了服务代码。 - firetrap

-4

你尝试使用了在清单文件中指定的 android:name 吗?

Android 清单文件:

<service 
             android:enabled="true" 
             android:name="UploadService" />

调用应该是这样的:
Intent intent = new Intent("UploadService");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接