Java 我能';我的广播接收器没有启动服务

Java 我能';我的广播接收器没有启动服务,java,android,service,android-intent,broadcastreceiver,Java,Android,Service,Android Intent,Broadcastreceiver,当广播接收器触发警报时,我正试图启动服务。我没有得到任何错误和代码运行,但它没有启动我的服务,我没有得到任何错误。这可能是我服务中的问题还是舱单中的问题 我注意到,当涉及到意图和上下文时,我经常遇到问题。我试着去阅读,但是我找不到一个网站能很好地解释它。有什么建议吗 public class Alarm extends BroadcastReceiver { @Override public void onReceive(Context context, Intent inte

当广播接收器触发警报时,我正试图启动服务。我没有得到任何错误和代码运行,但它没有启动我的服务,我没有得到任何错误。这可能是我服务中的问题还是舱单中的问题

我注意到,当涉及到意图和上下文时,我经常遇到问题。我试着去阅读,但是我找不到一个网站能很好地解释它。有什么建议吗

public class Alarm extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(1000);

        Intent myService = new Intent(BackgroundService.class.getName());
        context.startService(myService); 
    }
}
**************清单*******

<service android:name=".BackgroundService" android:process=":remote">
    <intent-filter>
    <action android:name="se.davidsebela.ShutMeUp.BackgroundService" />
    </intent-filter>
</service>

<receiver  android:process=":remote" android:name="Alarm"></receiver>
</application>

这仅仅通过一个动作就创造了一个新的意图。操作是BackgroundService类的名称。当您使用
startService()
时,这将不起作用

而是使用获取类和上下文作为参数的意图构造函数:

Intent myService = new Intent(context, BackgroundService.class);

明白了,-D类。现在我知道问题了。你建议的代码有效

Intent myService = new Intent(context, TestService.class);
context.startService(myService);
但问题是我的服务已经在运行了。我以为它被杀死了,但只有计时器停止了,服务还在运行。因此,当执行上面的两行时,它基本上没有任何作用


现在我必须找出如何真正杀死它:)

我得到了同样的结果。在Eclipse中看起来不错,但当我在Android emulator中运行代码时,服务从未启动,也没有出现错误:(@DavidSebela您确定您的广播接收器已正确注册并接收到适当的广播吗?请尝试在
onReceive()中添加断点或日志语句)
并查看您是否在那里结束或它是否没有被调用。在您的服务onStart/onStartCommand中也执行同样的操作。我知道会触发报警,并且onReceive会运行。我有Log.d();内部,但我在发布时已将其删除。我的服务也做了很多,我知道它没有启动。但我的服务中有大量的内容,如timertasks和其他内容。因此,我将创建一个新服务来测试更多内容。感谢加载:)嗯,好的。我猜服务也在AndroidManifest?中正确注册,并使用stopSelf();让一切都像我想的那样运作。谢谢你
Intent myService = new Intent(context, TestService.class);
context.startService(myService);