“无法启动服务意图”从 Android 中的 Activity 启动服务时出错

发布于 2024-10-09 13:31:54 字数 3064 浏览 0 评论 0原文

当尝试在我的“MyActivity”活动上使用复选框来启动名为​​“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));
                }
            }
        });
    }
}

我的清单文件确实具有以下内容,其中我还尝试了完整的命名空间、短名称、在另一个搜索中使用意图过滤器等。我并不是说那里的内容是正确的。

<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 编程和一般编程(但学习),所以我确信这是用户错误,并且可能是其他人的常识。

I see the following error in DDMS when trying to use a CheckBox on my MyActivity" activity to start a service called "MyService":

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

I used the tutorial http://developer.android.com/resources/tutorials/views/hello-formstuff.html and added the provided code to the end of my onCreate() method. I have the classes specified separately in MyActivity.java and 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));
                }
            }
        });
    }
}

My manifest file does have the following in which I've also tried the full namespace, short name, using an intent-filter per another search, etc. I'm not saying what is there is correct. I just left it at a stopping point.

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

And lastly, my service which I've decided to break down to it's bare minimum:

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

I'm very, very, very new to Java/Android programming and programming in general (but learning) so I'm sure this is user error and probably common sense to everyone else. Any suggestions would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神爱温柔 2024-10-16 13:31:54

我不断地挖掘,正如我所想的那样,我犯了一个明显的菜鸟错误。在 AndroidManifest.xml 中,我有 <服务> <之后的声明应用>而不是嵌套在其中。

I kept digging around and, as I figured, I was making an obvious rookie error. In AndroidManifest.xml, I had the < service> declaration after < application> instead of nested inside it.

随梦而飞# 2024-10-16 13:31:54

您不需要编写意图过滤器,因为您正在显式启动服务。
如果您是 Android 新手,请使用以下链接,这将对您非常有帮助。
它也有服务示例。
http://saigeethamn.blogspot.com/2009/08/android -developers-tutorial-for.html

You need not to write intent filter because you are starting service explicitly.
If you are new to android use following link it will be very helpful for you.
It has service example too.
http://saigeethamn.blogspot.com/2009/08/android-developers-tutorial-for.html

与之呼应 2024-10-16 13:31:54

清理你的manifest.xml中的行

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

clean up the line in your manifest.xml

      <intent-filter>
             <action android:name="com.example.android.myprogram.MyService"> 
            </action>
        </intent-filter>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文