在Android中从Activity启动服务时出现“无法启动服务Intent”错误

8

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

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

我使用了教程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));
                }
            }
        });
    }
}

我的清单文件确实包含以下内容,我也尝试过完整的命名空间、短名称以及使用另一个搜索的intent-filter等方式。我不是说那里的内容是正确的。我只是把它留在了一个停止点。

<service android:name=".MyService">
   <intent-filter><action android:name="com.example.android.myprogram.MyService"></action>
   </intent-filter>
</service>

最后,我决定将我的服务简化到最基本的层面:

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
    }
}

我在Java/Android编程和编程方面非常、非常、非常新手(但正在学习),所以我相信这是用户错误,对其他人来说可能很简单。任何建议都会很棒。


你能把服务的代码粘贴过来吗? - Cristian
Cristian,感谢您的回复。代码已添加。问题:我是否应该能够使用它并获得成功的结果?http://android.kgmoney.net/2010/05/08/creating-a-simple-android-service-for-background-processing/。这也不起作用。 - capitalf
4个回答

17

我一直在寻找问题所在,正如我想的那样,我犯了一个显然的新手错误。在AndroidManifest.xml文件中,我把<service>声明放在<application>之后,而不是嵌套在其中。


今天你显然为我节省了很多时间。谢谢你。 - Dam

0

清理您的manifest.xml文件中的行

      <intent-filter>
             <action android:name="com.example.android.myprogram.MyService"> 
            </action>
        </intent-filter>


0

谢谢回复。实际上,我在发布后就已经删除了那行代码,因为我记得那只是为第三方应用程序使用我的服务而设置的。不幸的是,我仍然收到错误提示。 - capitalf

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