简体   繁体   English

Android BroadcastReceiver在后台接收

[英]Android BroadcastReceiver Receive in Background

I am registering a BroadcastReceiver to an activity. 我正在向活动注册一个BroadcastReceiver。 It works fine but does not work when the activity is stopped. 活动正常,但在活动停止时不起作用。 ie code inside the onReceive method is not executed when it receives a broadcastmessage (as the activity is stopped). 即onReceive方法中的代码在收到广播消息时不会执行(因为活动已停止)。

Here is my code that I call 这是我调用的代码

PendingIntent sentPI = PendingIntent.getBroadcast(act,0,new Intent(sms_sending).putExtra("s_id",id),0);
act.registerReceiver(new SentBroadcast(id, act), new IntentFilter(smssent));

I think I have to start a service to receive it in the background, but I'm not getting how to do that. 我认为我必须启动一项服务才能在后台接收它,但是我没有得到如何做的信息。

Edit 编辑

Here's how I registered in application. 这是我在应用程序中注册的方式。

<receiver android:name="com.smsapp.sendsms$SentBroadcast" android:exported="false">
        <intent-filter>
            <action android:name="com.smsapp.sendsms.SendSMS.smssent" />
        </intent-filter>

You declared receiver in Manifest already, but later you still register it again on Activity? 您已经在Manifest中声明了接收者,但是稍后您仍然在Activity上再次注册它吗? Did you unregister that receiver in onStop or onDestroy? 您是否在onStop或onDestroy中注销了该接收器? If yes, please just remove register receive in Activity, register in Manifest is enough. 如果是,请删除在活动中的注册接收,在清单中注册就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM