Answer the question
In order to leave comments, you need to log in
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);
Answer the question
In order to leave comments, you need to log in
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);
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);
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 questionAsk a Question
731 491 924 answers to any question