I am working on two applications of same company. My requirement is "When I install two fresh applications and do login to one application, it should automatically login to another application. And when I logout from one application, same user should also log out from another application."
For example. I have 2 applications.
- Facebook Messenger.
When I log into Facebook from user "John", so when I open Facebook messenger, it is already logged in with user "John". And when I log out from Facebook app, that user should also logged out from Facebook Messenger. And vice versa.
How can I achieve this functionality in Android? Actually this functionality is working in below oreo devices.
Here is what I tried to implement.
Code
Registering receiver in Application class of both applications.
ASampleActionReceiver mASampleActionReceiver = new ASampleActionReceiver();
IntentFilter filterAction = new IntentFilter();
filterAction.addAction("com.sample.example.receiver.operation");
registerReceiver(mASampleActionReceiver, filterAction);
In Manifest of both applications.
<receiver android:name=".receiver.ASampleActionReceiver">
<intent-filter>
<action android:name="com.sample.example.receiver.operation" />
</intent-filter>
</receiver>
We have intent service in both applications. Intent service will start by receiver
<service
android:name="com.sample.example.services.SampleOperationService"
android:enabled="true"
android:exported="false" />
And a Receiver ASampleActionReceiver.java
public class ASampleActionReceiver extends BroadcastReceiver {
Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
AdoddleLog.d("LOG", intent.getExtras().getString("loginData"));
Intent mIntent = new Intent(mContext, SampleOperationService.class);
mIntent.putExtras(intent);
mContext.startService(mIntent);
}
}
But device which has android 8.0+ OS , Receiver not working, because https://developer.android.com/about/versions/oreo/background https://developer.android.com/about/versions/oreo/android-8.0-changes#back-all
So how can I achieve this?