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

fir*_*rap 29 android android-intent android-service

我有同样的问题在这里描述,但我无法理解什么是错的.

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

编辑 我已经在源代码中将我的包从连接更改为服务,抱歉让人感到困惑

我的清单 `

<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>
Run Code Online (Sandbox Code Playgroud)

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的主要.

Intent start = new Intent(".service.MoodyService");
        this.startService(start);
Run Code Online (Sandbox Code Playgroud)

并尝试过

Intent intent = new Intent(this, MoodyService.class);
        this.startService(intent);
Run Code Online (Sandbox Code Playgroud)

并尝试了完整的道路

<service
    android:name="com.example.moody.service.MoodyService"
    android:icon="@drawable/ic_launcher"
    android:label="@string/moody_service" >
Run Code Online (Sandbox Code Playgroud)

fir*_*rap 26

解决了

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

这不起作用:

.yourPackage.YourClass
Run Code Online (Sandbox Code Playgroud)

但这确实有效:

 yourPackage.YourClass
Run Code Online (Sandbox Code Playgroud)

主要是:

Intent intent = new Intent(this, MoodyService.class);
        this.startService(intent);
Run Code Online (Sandbox Code Playgroud)

但它违背了文档的内容:

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

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


Fra*_*les 16

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

<service android:name="com.x.x.serviceclass"></service>
Run Code Online (Sandbox Code Playgroud)


小智 6

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

<service  
      android:name=".MyService"  
      android:enabled="true" >
</service>
Run Code Online (Sandbox Code Playgroud)

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

startService(new Intent(getApplicationContext(), MoodyService.class));
Run Code Online (Sandbox Code Playgroud)