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

问题描述 投票:28回答:8

我有同样的问题描述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" >
android android-intent android-service
8个回答
26
投票

解决了

我删除了清单中包名称开头的句点并且它有效,换句话说:

这不起作用:

.yourPackage.YourClass

但这确实有效:

 yourPackage.YourClass

主要是:

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

但它违背了the documentation所写的内容:

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

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


16
投票

该服务也必须包含在清单中:

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

4
投票

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

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

2
投票

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

  android:name ="connections.MoodyService"

要么

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

调用服务做

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

1
投票

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

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

1
投票

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

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

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

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

0
投票

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

如果没有,试试吧。

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


-3
投票

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

Android清单:

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

这个电话会是这样的:

Intent intent = new Intent("UploadService");
© www.soinside.com 2019 - 2024. All rights reserved.