0

I have an app A and app B, the apps are signed with the same certificate. App A has a checkbox preference, app B needs to know is the preference checked or not.

I came up with an idea to broadcast an intent with needed Boolean value been set on every change of the checkbox state in app A. Having a unique action for this intent together with a custom permission that has signature protection level should guarantee that only app B will receive such intent in its BroadcastReceiver.

Together with it I considered using ContentProvider, which looked like not the best option to me. for using it I need to override a bunch of CRUD methods that won't be used in my particular case. Only one flag will be shared, so there is no need to communicate with a database. Also I looked at binding to a Service, which is located in app B, from app A using a Messenger. In this case I get a Handler, and when a user checks or unchecks the setting, I can send a message with an appropriate argument to app B. But such approach brings some problems to the table with binding/unbinding to the service (leak connection possibility or excess connections).

The problem I faced with is that app B must have a possibility of asking the state of the checkbox in app A. For example if the user reinstalls the app B.

In other words, I can notify app B about changing of the checkbox state from app A, but how I can ask app A about that state from app B?

Sending sticky broadcasts from A to B would fix the problem, but sticky ones are deprecated in API level 21 because of possible security vulnerabilities.

  • Maybe use SharedPreferences with a WORLD_READABLE mode? Something like: http://stackoverflow.com/a/11949750/1426565 – Cruceo Jul 13 '16 at 22:02
  • 1
    @Guardanis: That never worked well and is officially banned as of Android N, as with all uses of `MODE_WORLD_READABLE`. – CommonsWare Jul 13 '16 at 22:03
  • @CommonsWare thanks for the info. I hadn't noticed that in the changes – Cruceo Jul 13 '16 at 22:05

2 Answers2

0

You can work with a configuration file format key:value and have you activity(ies) read this file, you can make this file invisible to user and visible to you apps (remember android device is a Linux therefor you can easily make a invisible file). you can also use broadcast to notify the activities that this configuration file changed and need be read again .

Another way you can do it is setting the sharing user id for both applications.

Adliano Alves
  • 311
  • 2
  • 6
0

I came up with an idea to broadcast an intent...

Now you need to maintain two copies of the data, which seems unnecessary and prone to reliability concerns. The exception would be if B might remain while A is uninstalled. In that case, you would need B to store its own copy of the boolean.

for using it I need to override a bunch of CRUD methods that won't be used in my particular case.

Override query() to return a MatrixCursor containing your boolean. Override the others to throw a RuntimeException.

Also I looked at binding to a Service, which is located in app B, from app A using a Messenger.

Or, have a Service in A that exposes an AIDL interface to return the boolean. Have B bind to the service to read in the boolean. If B needs to find out about real-time state changes in the boolean, add a listener to the AIDL interface.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491