A
A
Anton Artyomov2016-12-20 17:14:00
Android
Anton Artyomov, 2016-12-20 17:14:00

How to pass data from Service to running Activity?

Hello. I've scoured the internet but couldn't find an answer. Maybe the question was worded wrong.
The point is the following. In PasswordActivity, the user enters a phone number, clicks the "receive code via SMS" button. At the same time, the field for entering the code from SMS is shown. At this time, with the available permissions, class SMSMonitor extends BroadcastReceiver waits for SMS. Receives. Parses and passes to class SmsService extends Service. SmsService processes the SMS body, extracts the necessary data, decides which activity to transfer them to. Passes, for example, to PasswordActivity like this:

Intent intent = new Intent(this, PasswordActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("sms_code", code);
startActivity(intent);

Thus, another PasswordActivity is launched in a new task. Then the user enters a new password and it, along with the verification code from SMS, is sent to the server. If the password change is successful, I do a finish() for the activity. The current PasswordActivity is closed, and under it is still that old PasswordActivity with a field for entering the code from SMS.
At the same time, in the manifest for PasswordActivity, I set the properties android:launchMode= "singleInstance" and something else ... I played with the Intent.FLAG_ACTIVITY_ flags ...
There are flags that do not seem to create a new task, but at the same time, PasswordActivity does not work neither onCreate nor onResume.
After intercepting SMS, I definitely need to continue working with the activity, because further user action is required.
In short, how do I pass data from the service to an already running activity that is not even paused?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Artyomov, 2016-12-21
@ArtyomovAnton

I solved the problem as follows: SMSMonitor no longer calls the SmsService that started the activity. SMSMonitor now sends a broadcast message:

Intent mIntent = new Intent("SmsMessage.intent.MAIN");
mIntent.putExtra("sms_code", code);
context.sendBroadcast(mIntent);

And in the onResume method of the PasswordActivity class, I register a BroadcastReceiver:
IntentFilter intentFilter = new IntentFilter("SmsMessage.intent.MAIN");
        mIntentReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                sms_code = intent.getIntExtra("sms_code",0);
                if (sms_code > 0) {
                    // Необходимые операции с элементами view и методами активити
                }
            }
        };
        registerReceiver(mIntentReceiver, intentFilter);

A
Alexey Grichenko, 2016-12-20
@Kalobok

I suspect that the issue is in the FLAG_ACTIVITY_NEW_TASK flag. You create not just a new activity, but a new process, and the old one remains hanging in the background. By the way, the documentation says that "singleTask and singleInstance - are not appropriate for most applications" - precisely because they can lead to strange results if used carelessly.
In a similar situation, the FLAG_ACTIVITY_CLEAR_TOP flag works fine for me. The called activity has singleTop in its manifest. True, I call it not from the service, but from another activity, but I don’t think that this is important. One more thing to keep in mind is that if your PasswordActivity already exists, then onNewIntent() can be called instead of onCreate() . It also needs to be redefined in order to properly process data from the service. Maybe that's why you didn't succeed when you tried to "play with the flags".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question