I have some trouble sending an Intent from a Service to an Activity. Here’s the code:
Intent intent = new Intent(context,
MessagingActivity.NewMessageReceiver.class);
intent.setAction(XMPPService.NEW_MESSAGE);
intent.putExtra("MESSAGE", body);
intent.putExtra("FROM", from);
context.sendBroadcast(intent);
context is a Service
Receiver code (it’s an inner class in the activity):
public class NewMessageReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(XMPPService.NEW_MESSAGE))
{
String message = intent.getStringExtra("MESSAGE");
String from = intent.getStringExtra("FROM");
// TODO: add screen switch
addMessage(true, from, message);
}
}
}
Android manifest entry:
<receiver
android:name=".MessagingActivity.NewMessageReceiver">
<intent-filter>
<action android:name="com.someapp.XMPPService.NEW_MESSAGE"/>
</intent-filter>
</receiver>
The problem is that although the Intent is sent, onReceive() method is not called.
Thanks, Ivan.
Try this
In the Activity