gpt4 book ai didi

java - 我无法从我的 BroadcastReceiver 获得 startService

转载 作者:行者123 更新时间:2023-11-29 22:15:53 25 4
gpt4 key购买 nike

当我的 BroadcastReceiver 触发警报时,我正在尝试启动我的服务。我没有收到任何错误并且代码运行,但它没有启动我的服务并且我没有收到任何错误。这可能是我的服务或 list 中的问题吗?

我注意到,在涉及 Intents 和 Contexts 时,我经常遇到问题。我试图阅读它,但我找不到一个网站可以很好地解释它。有什么建议吗?

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

****************** list *******

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

最佳答案

Intent myService = new Intent(BackgroundService.class.getName());

这将创建一个仅包含一个操作的新 Intent 。该操作是 BackgroundService 类的名称。当您使用 startService() 时,这将不起作用。

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

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

关于java - 我无法从我的 BroadcastReceiver 获得 startService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8456856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com