问题描述:

I am registering a BroadcastReceiver to an activity. It works fine but does not work when the activity is stopped. i.e code inside the onReceive method is not executed when it receives a broadcastmessage (as the activity is stopped).

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? Did you unregister that receiver in onStop or onDestroy? If yes, please just remove register receive in Activity, register in Manifest is enough.

相关阅读:
Top