【问题标题】:Unable to start service Intent: not found无法启动服务意图:未找到
【发布时间】:2013-09-30 23:32:41
【问题描述】:

here 描述了同样的问题,但我不明白出了什么问题。

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

编辑 我已将源代码中的包从连接更改为服务,很抱歉造成混淆

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

service 是包,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"'作为包名称
  • @ShakeebS 我换了一个新包,忘记更新帖子

标签: android android-intent android-service


【解决方案1】:

已解决

我删除了清单中包名开头的句点,它起作用了,换句话说:

这不起作用:

.yourPackage.YourClass

但这确实有效:

 yourPackage.YourClass

主要是:

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

但这与the documentation中所写的内容背道而驰:

android:name 实现服务的服务子类的名称。这应该是一个完全限定的类名(例如, “com.example.project.RoomService”)。但是,作为简写,如果 名称的第一个字符是句点(例如,“.RoomService”), 它被附加到指定的包名称中 元素。一旦你发布你的应用程序,你不应该改变它 名称(除非您设置了 android:exported="false")。

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

【讨论】:

  • 完全错过了这一点,尽管这是您的错误。当您在清单的根目录中包含 package="com.example.moody" 并编写 android:name=".service.MoodyService" 时,系统将查找名为“com.example.moody.service”的服务。穆迪服务”。但是您在 q 顶部的输出显示“com.example.moody.connections.MoodyService”,因此我认为有时会出现复制粘贴错误。
  • 仍然有一些错误为什么你的错误在包'act = com.example.moody.connections.MoodyService'但是你有'"com.example.moody.service.MoodyService"'作为包名称
  • 是因为我更新了代码中的包并编辑了问题,但忘记更新问题中的包名称
  • 当我重命名我的服务类时,我也遇到了同样的情况。必须在清单中手动更改它
  • 在我的情况下这是相反的。我在清单中从“yourPackage.YourClass”更改为“.yourPackage.YourClass”并且它有效。我意识到“。”是一件大事 :) @firetrap 感谢您的建议 :)
【解决方案2】:

服务也必须包含在Manifest中:

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

【讨论】:

  • 如果它在 androidTest 目录中呢?那里需要单独的 Manifest 吗?
  • 谢谢。我忘了声明服务,这就是我遇到这个问题的原因。
【解决方案3】:

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

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

并尝试编写 getApplicationContext() 而不是“this”关键字

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

【讨论】:

  • enabled 是干什么用的?
【解决方案4】:

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

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

【讨论】:

  • 我试过没有区别同样的错误,我做了什么:“Intent intent = new Intent(this, MoodyService.class); this.startService(intent);”也改成新的包,但结果相同,它只是在那里,因为它不再使用该包
  • 这正是我遇到的问题。 AndroidManifest.xml 很好,但我在 BroadcastReceiver 中传递了 onReceive() 接收到的意图。
【解决方案5】:

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

  android:name ="connections.MoodyService"

或

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

调用服务做

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

【讨论】:

  • 是的,正如我在自己的回答中提到的那样,您是对的,但文档说:“但是,作为速记,如果名称的第一个字符是句点(例如,“.RoomService “),它被附加到元素中指定的包名称”所以我有句号,因为服务是包
  • connection 是包名或service 是包名
  • 我已经更新了问题,MoodyService.java 在包中 - “连接”,现在它在包中 - “服务”,我的错误不是包名称我只是忘记更新问题
【解决方案6】:

我的服务在“服务”包中,我的清单服务是这样的;

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

【讨论】:

    【解决方案7】:

    您是否在服务中创建了一个空的构造函数?

    如果没有,请尝试。

    也不确定你是否可以这样使用 Intent。您可以尝试“new Inten(this, MoodyService.class)”构造。

    【讨论】:

    • 是的,我做到了,也尝试过:Intent intent = new Intent(this, MoodyService.class); this.startService(intent);我已经用服务代码更新了我的问题
    【解决方案8】:

    您是否尝试使用您在 Manifest 中指定的 android:name?

    Android 清单:

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

    电话会是这样的:

    Intent intent = new Intent("UploadService");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 2018-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多