How to get a value from another android application? - android

I'm building 2 applications.
The first application, on start, asks a value (for example an integer) to the second application, which may or may not be installed and may or may not be running.
I don't think shared files or preferences are the best option, because I want to provide different output when the second application is present or not.
Should I use Intents? Content provider? Anything else?

Use Intents and Activities, if you want to retrieve a user entered value, or something that requires user interaction.
ContentProvider would also work, depending on your requirements.

I solved by creating an Intent with putExtra and finish() (to return to first app). 4 lines of code on each app.

Related

What is the way to pass a Boolean value between applications in Android?

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.
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.
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.

Sharing small amount of data between apps

I have couple of applications that implements some login logic. If lets say one application is logged to some_account#gmail.com I want that all of these applications be logged to some_account#gmail.com. If I logout I want to all application do the same. But I don't want to immediately do the same. Application itself can handle it, but it need to know if some other application is already logged in and if yes just log in for the same email address as this app. So I need to know what is the email address for which other app is logged. I need to store one string.
First I was thinking about SharedPreferences, but this is rather bad idea because there are other options (and stackoverflow is full of bad example of SharedPreferences usage between processes). Despite it I tried this. Set up sharedUserId on all apps, called createPackageContext and eventually try to get preferences. But I cannot read from it. Always I got null, even if I used Context.Mode_WORLD_READABLE - which is deprecated by the way.
Ok, lesson learned do not use SharedPreferences for that (I suppose). But everything what I need now is to store single string somewhere where it could be read by other my apps.
Maybe I should use ContentProvider? But seriously... for one string?
What is the other option? I am sure that for so simple operation I really don't need Service or ContentProvider, but I actually haven't got idea how to do that.
You could use a broadcast receiver. All you would have to do is send a broadcast to application B when the data changes in application A. Then application B can handle the data in the background, and store it however you need to. It might be a bit of over kill, and there could be a better way to do it, but it would work.

Android Is it posible to get an Activity's preference value from a Service?

Given that a Service may be running even when the launching activity is destroyed,
and also that data is passed usually using the extra bundle along the intent,
I wonder if the Service is able to directly access the activity's preferences.
My guess is no, it cannot. But it would save me writing a chunk of code if it can do it.
Assuming that You're asking about SharedPreferences. If so, then documentation is clear (by link above):
Note: currently this class does not support use across multiple
processes. This will be added later.
So, currently, if Your service is running in a separate process it's not possible. In that case I would suggest to store Your preferences not in a SharedPreferences, but in some base which could be accessible via specific ContentProvider.
P.S. Actually I tried access SharedPreferences from multiple processes and it has worked (at least in my case), but I've decided to use another way because of the documentation mentioned in the answer.
If you mean SharedPreferences then you can just call getSharedPreferences with the same name.

ANDROID - class Application

I'm trying to understand how the Application class.
I've noticed that need to declare it in <application> manifest within the tag and then can access the variables in other classes as they were global variables. And even out of the application the value of these varieties do not change.
However, if you unplug the phone, the next time you turn it on and start applying the value of the variables returned to its initial state.
I wonder if you can maintain the state of variables when we turned off the phone and reconnect it?
Application data is available as long as your application is "active". When the OS decides to terminate it to clear memory, so goes your application data (you typically don't control when this happens, as per the mobile development good practices: the OS decides on its own), and it's not persisted for the next time you start the app. So anything you store in the Application should be stored again each time the app is started.
It should be used to keep short-term data available to you. A good use case is when you need to access a complex data structure from multiple activities: it's not possible to use bundles for that. You can generate your complex data structure in your start activity, store it in the application, and then retrieve it in any other application that may need it.
But you should not use it for long-term persistent data. For that, the best is to use a SQLite database.
I'm not sure I fully understand what you mean, but it seems like you want to use Shared Preferences.
try this Question: Android - How Do I Set A Preference In Code

Sharing an object between activities

I have a Weather app with four Activities. The main/launcher activity is 'invisible' using...
android:theme="#android:style/Theme.Translucent.NoTitleBar"`
...and is simply used to do a few checks (whether this is a new install, whether a network connection is available etc) before firing off one of the other Activities. The other Activities are UI-oriented - two simply display weather data pulled from a website and the third to provide a location 'picker' so the user can choose which area to show the weather for.
However, all four activities make use of a WeatherHelper object which basically does everything from checking for available SD card storage to maintaining preferences and pulling/formatting website pages.
So, my question(s)...what is the best way to have one instance of WeatherHelper which can be used by multiple activities and where/how are best to create it in my case?
I've been an OO programmer for a lot of years but I'm very new to Android and the design concepts - I've read a lot on the Android Developers site over the past weeks but I've stalled trying to decide on this.
Any ideas gratefully received.
I would store shared information in you Application object. Subclass this and add any extra initialization and data there. You can get your application using getApplication() from your activity, which you can cast to your specialized version and access the shared data.
I would also avoid launching the special startup activity if possible and do the work in your Application's onCreate() override.
Well, your question has been answered, but it seems like it would be much simpler to instantiate your WeatherHelper object in the onCreate() of the Activity that has the launcher intent, and make the WeatherHelper static.

Categories

Resources