0

I IntentService that I would like to send message to the main Activity it is nested in. I am using a broadcast receiver to broadcast the message I got from the IntentService as such:

public static class ResponseReceiver extends BroadcastReceiver {
        public static final String ACTION_RESP = "com.mypackage.intent.action.MESSAGE_PROCESSED";
        @Override
        public void onReceive(Context context, Intent intent) {
            String text;
            text = intent.getStringExtra(RegistrationIntentService.PARAM_OUT);
            regid = text;
        }
    }

I have registered the receiver in the Oncreate method of the main Activity. How can I send the "text" in this case? It is weird that regid in this case is null while "text" has the string data I wanted.

3 Answers 3

1

you can user result receiver with intent service to get the result into activity or fragment, follow the following links,

http://sohailaziz05.blogspot.in/2012/05/intentservice-providing-data-back-to.html

Sign up to request clarification or add additional context in comments.

Comments

0

In your service you do

Intent intent = new Intent();
intent.setAction(yourActionToMatchBroadcastReceiverIntentFilter);
intent.putExtra(RegistrationIntentService.PARAM_OUT, yourText);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

In activity register your BroadcastReceiver in onResume() and unregister it in onPause(). Whenever your activity is active the receiver will receive intents from your IntentService.

EDIT

public static class ResponseReceiver extends BroadcastReceiver {
    MapActivity activity;
    public ResponseReceiver(MapActivity activity) {
        this.activity = activity;
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        activity.regid = intent.getStringExtra(RegistrationIntentService.PARAM_OUT);
        // do whatever you need here
    }
}

6 Comments

I did and received the intent extras in the broadcast receiver. My problem is sending the "text" in my question to the main activity. The broadcast receiver is a nested class in main activity.
You can pass an activity reference to your broadcast receiver when you register it: new ResponseReceiver(activity);. In onReceive you can do: activity.regid = intent.getStringExtra(RegistrationIntentService.PARAM_OUT);
Hi, @Gennadii. I added these constructors private MapActivity ma; public ResponseReceiver(){} public ResponseReceiver(MapActivity act){ this.ma = act; } and onResume I did the following:IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP); filter.addCategory(Intent.CATEGORY_DEFAULT); receiver = new ResponseReceiver(this); registerReceiver(receiver, filter); Unfortunately, When I assign the "text" in the onReceive method: MapActivity.regid = text, regid is null. "text" is populated with id like I am supposed to get.
I don't follow you.. In the first comment you said you received the "text" but you wasn't able to set it to activity. I showed you how to set it to your activity and now you say it is still null? I think I'm missing something here
When I debug I am getting "type is unknown for 'ma'" in the onResult method of the broadcast receiver. fyi the string regid has a global scope and is declared within the parent class, mapactivity.
|
0

When registering, this was what worked for me

IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP);
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        receiver = new ResponseReceiver();

        LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter);

as opposed to

registerReceiver(receiver, filter);

@Override
        public void onReceive(Context context, Intent intent) {
            final String text = intent.getStringExtra(RegistrationIntentService.PARAM_OUT);

            button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    //access the string text and send it to backend
                }
            });
        }

I hope this will help someone. Sending the string like suggested in the comments didn't work for me. I was getting nullpointerexception at that specific line where I assigned ma.regid = text;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.