从Android中的Activity启动服务时出现"无法启动服务Intent"错误

cap*_*alf 8 service android android-intent android-activity

当我尝试在MyActivity上使用CheckBox"活动来启动名为​​"MyService"的服务时,我在DDMS中看到以下错误:

W/ActivityManager(   73): Unable to start service Intent { cmp=com.example.android.myprogram/.MyService }: not found
Run Code Online (Sandbox Code Playgroud)

我使用了教程http://developer.android.com/resources/tutorials/views/hello-formstuff.html,并将提供的代码添加到我的onCreate()方法的末尾.我在MyActivity.java和MyService.java中单独指定了类.

package com.example.android.myprogram;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;


public class MyActivity extends Activity {
    private static final String TAG = "MyActivity";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
        checkbox.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Perform action on clicks, depending on whether it's now checked
                if (((CheckBox) v).isChecked()) {
                    // TODO: Add code to START the service
                    Log.d(TAG, "startService from checkbox");     
                    startService(new Intent(MyActivity.this, MyService.class));
                } else {
                    // TODO: Add code to STOP the service
                    Log.d(TAG, "stopService from checkbox");     
                    stopService(new Intent(MyActivity.this, MyService.class));
                }
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

我的清单文件确实有以下内容我已经尝试了完整的命名空间,短名称,使用每个其他搜索的intent-filter等等.我不是说什么是正确的.我把它留在停止点.

<service android:name=".MyService">
   <intent-filter><action android:name="com.example.android.myprogram.MyService"></action>
   </intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)

最后,我决定将其分解为最低限度的服务:

package com.example.android.myprogram;


import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {
    private static final String TAG = "MyService";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate");
        //code to execute when the service is first created
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
        //code to execute when the service is shutting down
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Log.d(TAG, "onStart");
        //code to execute when the service is starting up
    }
}
Run Code Online (Sandbox Code Playgroud)

我是非常非常非常新的Java/Android编程和编程(但是学习)所以我确信这是用户错误,并且可能是其他人的常识.任何建议都会很棒.

cap*_*alf 17

我一直在四处挖掘,正如我想的那样,我正在犯一个明显的菜鸟错误.在AndroidManifest.xml中,我在<application>之后有了<service>声明,而不是嵌套在它里面.