Nearby Connections advertise with application context - android

I'm using Nearby Connections framework for Quiz Game application. Also, I'm using MVVM design pattern along with live data and room. When I call Nearby.getConnectionsClient() in activity, I can pass this as argument or applicationContext as argument and everything works. But I don't want handle networking in activity, I want do this for example in view model (or maybe some singleton). But when I use AndroidViewModel(application) and as argument pass application.applicationContext, which is same application context like in activity, it doesn't work. When I'm trying to advertise, I get com.google.android.gms.common.api.ApiException: 13: ERROR. I have no idea what is difference and why this is not working. Plus when I am using activity context and rotate a phone, it got destroyed. So I need start advertising again?
I'm working on quiz app and I want to create lobby activity, where you can find nearby HOSTS and join. Then I want to have access to this connection in next activities, where game is playing. So also I don't have idea how to work with this connection in multiple activities.
Thanks for help.

Using the application context definitely works. Try using the application object itself instead of Application.applicationContext

Related

Sending data from an activity to an object on a 3rd party activity

In the course of creating an android library, I've learned I need to be able to open an activity from a generic library object and have the activity pass back data to the the library object. Normally, I would simply use startActivityForResult and call it a day, but in this case the library object will be using the client application's context. As such, any result would be sent to the client application and not to the library object.
So the flow would be something like this: Client instantiates our library object -> the library object determines it needs to present its own activity and does so -> library's activity returns data to the library object which can continue processing
I've tried a couple of different solutions but none seem to produce the desired results.
fragments - the issue I ran into here is that the fragment is tied to an activity which means it would need to somehow be tied to the client's activity in order for our object to get what it needs. So for our purposes this doesn't make sense to use.
Temporary splash screen - this is the route we're currently leaning towards since a basic splash screen would allow us to leverage our object on an activity we owned which could then call the activities it may need along the way and then return a response to the client's app in the activityForResult. The drawback of this design is that we were hoping to leverage a set of events which the client could code to when we fire them off. However this can be worked around if needed.
Also looked into leveraging the sharedPreferences but the issue there would be that when we returned to the client's activity we'd somehow need to "kick" the library to continue working. We don't want our clients to have to make multiple calls into our library. And spinning off a background thread to "poll" feels like very bad practice in this situation.
So what I'm looking for is whether the 2nd approach is really the only way to solve this or if there is another way in android development which I'm currently unaware of?

Android - Determine App Launches and Total Time as Top Activity

I am working on a solution or code that can be embedded inside of an Android APK to track how many times the app has been launched and how long the app has ran for. I know one way to do this is using the ActivityLifecycleMethods in API 14 and in lower versions of Android having code placed in all Activity Lifecycle events or by providing a base Activity class.
1) Is there a way to hook the ActivityLifecycleMethods without the developer having to make any changes to their code outside of dropping additional code into their App?
I believe this answer is no because even with an Enum Singleton it is not loaded until it is referenced. Also the Enum Singleton will go away once the activity is changed since a different class loader is used when activities change.
If I wanted to keep the Enum Singleton around would it be possible to store a reference to the applicationContext and thus it wouldn't be removed when the Activity changes? Is that what google means by
"There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton." on http://developer.android.com/reference/android/app/Application.html
2) I am not a fan of this solution for older API versions. It seems very likely developers could forget to modify their Activity Lifecycle methods or forget to inherit from the created BaseActivity. Are there any other unique solutions for these older platforms? Is there any other approaches that can be done to determine when an activity isn't running? Could any of the following work:
a) User a class loader to ensure the base activity with the proper metrics are always used
b) Implement some type of heart beat. Will a timer stop working if the app is paused or killed? Is there some other way? Could the ActivityManager be used?
You have many Analytic Agents like Flurry to do that.
When ever you want to track an event, you will add it to flurry and inturn it syncs with server after specific time.
You may use the same logic.
Better create a library file with following features:
Start Application
End Application and report time to db.
Track a specific event count and update to db.
Sync the data to server you like to.
Call appropriate events from your app.

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.

Android link to another package's function

I've got two android apps, one with a public void. How would the other app call this function?
You can not directly call the functions in another application. There are two ways to to achieve something like this.
The more complicated way would it be to have a service running that both your apps bind to to communicate. This would only be the way to go if you have frequent data sharings etc.
Another method is to use startActivityForResult. This will redirect the user to the activity of the other application and if the activity is finished you will be returned to your old activity and the set result is given to you in onActivityOnResult

Keeping a Bluetooth connection across activities on Android [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Android: How to pass a Bluetooth connection to another Activity?
I have an application in which I am going to be transferring data via a Bluetooth connection.
In my proof of concept app, I was able to put the Bluetooth communication in a different thread and keep all of my other work on the same activity (using ViewFlipper).
However, for my next iteration, I will need to use several different "screens" (activities). The flow would go something like this:
My initial activity will connect up to the Bluetooth device I will be getting the data from.
One activity off of the "main" activity will be to get a list of data from the BT device and display it in a list view. Then as I click on an item I will need to get a more detailed view of the data (which is shown in yet ANOTHER activity but also has to access the BT connection).
There will be additional, specific activities off of the "main" activity.
So my question is how is the best way to manage that, where I initiate the Bluetooth connection ONCE and don't have to initiate it again for each activity?
As Alex said store a controller for the connection as a member of a subclass of a custom application class.
You have to make your own application class that extends the android application class and register this class in your manifest. Now you can get the Application with getApplication and cast it to your subclass. Now you can access the member variables of your Application class.
Some other possibilities are described in the android documentations.
I would not use a static field in your application class. There is only one application class anyway and you can be sure that the whole application object won't be destroyed while your app is running. Some authors of Android books state that this is not that certain for static variables.
Store it as a static field in Application subclass (and instantiate with application context if required).

Categories

Resources