从发送广播接收机的意图在Android中正在运行的服务 [英] Sending an intent from broadcast receiver to a running service in android

查看:109
本文介绍了从发送广播接收机的意图在Android中正在运行的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的广播接收器。

Here is a my broadcast receiver.

public class SmsReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context ctx, Intent intent) {
            Log.d(DEBUG_TAG, SmsReceiver.class.getSimpleName()+ " action: " + intent.getAction());
            // here is codes for sending intent to my running service 

    }  }

下面是广播接收器和服务节点在我的清单XML文件。

here is broadcast receiver and service nodes in my manifest xml file.

<service android:name=".SMSGatewayService" />

<receiver android:name=".SmsReceiver">
    <intent-filter>
         <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

我怎么能发送意图从广播接收机我正在运行的服务。 在此先感谢。

How can i send intent from broadcast receiver to my running service. thanks in advance.

推荐答案

在理想情况下,你没有正在运行的服务。服务应该只在内存中时,他们正在做的事情,而赔率是你的code不会做任何事情时,短信到达。

Ideally, you don't have a running service. Services should only be in memory when they are doing something, and odds are your code won't be doing anything when an SMS arrives.

话虽这么说,你的接收器可以调用 startService()。这传递了意图 onStartCommand()的服务,无论是已经在运行与否。理想情况下,你的服务是一个 IntentService ,一是设计与命令模式工作,并关闭时,它完成处理命令。

That being said, your receiver can call startService(). This passes an Intent to onStartCommand() of the service, whether it is already running or not. Ideally, your service is an IntentService, one designed to work with the command pattern and to shut down when it is done processing a command.

另外,由于短信即可到达,而该设备是睡着了,你可能会需要使用一个 WakeLock ,以确保设备保持清醒足够长的时间控制传递给提供的服务以及服务做到不管它是它在做什么。这个问题的一个方法是使用我的 WakefulIntentService ,它封装起来在 IntentService + WakeLock 模式。

Also, since SMS messages can arrive while the device is asleep, you will probably need to employ a WakeLock to ensure the device stays awake long enough for control to pass to the service and for the service to do whatever it is it is doing. One approach for this is to use my WakefulIntentService, which wraps up the IntentService+WakeLock pattern.

这篇关于从发送广播接收机的意图在Android中正在运行的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆