Android IBeacon Library: How to not directly use an activity - android

The premise is I am writing a library that in itself uses and implements the IBeacon SDK.
My code has to implement that SDK, does things and if someone uses my library they dont have to interact with this SDK at all, so I'm making a higher level codebase you could say.
Now my code cannot use any activities by itself, its just a library, another user will then implements my code. So my code needs to consume the IBeacon events and do something with it without being an activity by itself.
So I would implement IBeaconConsumer myself and then the person using my library would just give me an activity / context and I would pass it to the IBeacon SDK.
Doing so results in the following problem (with proper mainfest code):
The activity binds its iBeaconManager object however onIBeaconServiceConnect never gets called.
See this stackoverflow question: Android IBeaconManager not connecting from activity
My vague assumption is this: the class implementing IBeaconConsumer is not an activity and its not the same class as the one I am binding the IBeacon service to and that might create the problem.
Its just very hard to debug since it all works, no errors, just no ibeacon activity - same behavior as if you forget to add the part in the manifest

If you want your IBeaconConsumer implementation to not be an instance of Activity or a Service, then you simply need to chain the bindService and unbindService methods to a valid ApplicationContext instance, and return that valid instance from the getApplicationContext method. This is not needed when you are implementing this interface with an Activity, because an Activity implements these three methods for you.
You can see an example of this in a related question here.

Related

Risks of using an Activity as a supplier of API

I am creating an application for Android which I want to turn into a library module and use its own logic as a common logic for several other application which differs in their UI. The library module got an Android service which can receive and send commands to a listener which is defined in the application itself (in my case in the main activity).
On the receiving side, The listener is the main activity of the application which then invokes callbacks according to the type of message from the service.
The issue is more with the sending commands to the service part. The API in question is the way of the application to send messages to the service. It simply gets the argument of the message to send and pass it along to the service.
The way I thought of doing it is making the library's main activity as the API which will allow both the sending and receiving events. The developer of a UI using this library will then simply need to make another module for the GUI alone, and make his main activity extend the library's main activity, thus being able to use the API and extend the listener's behavior.
What I want to know is possible technical issues, and not an opinion if the design itself is the best possible or not - Is there a problem with this design? For example issues with Android disposing of the activity? Any other issues?
The main risk with building your APIs based on the Activity is that you are basically tying your API to the activity lifecycle, I assume an API like that for messaging should be available on both the main and UI threads. I prefer using interfaces for designing API's. Dagger is very helpful for creating singletons which can be injected anywhere.

Calling methods on activity events

I am writing a simple Android library. I have a couple of methods that I would like executed when certain activity events happen - like after activity got loaded or when the activity gets paused.
One way I could think of was to create a class that extends activity, and write my methods there, and then have "target" activities extend that class. This was all methods get called
But if the end user is extending some other activity already, this method won't work. Is there a better alternative?
If you need only provide support back to API 14 or higher, you might be able to make use of the application level activity lifecycle callbacks - see the Application.ActivityLifecycleCallbacks interface for details.
To make use of this you need to register an instance of this interface with an Application instance, using the registerActivityLifecycleCallbacks method. One way to get that would be to have the developer using your library initialise your library by passing their Application instance to it. This is what I do in an SDK that I maintain, and it seems to work nicely.

How to organize Android App code?

First of all: I am rather new to Android App programming and I have a rather basic question:
Already with the sandbox app I am currently working on, the code in the Activity class get quite huge because all the callback methods / listeners (click listener, callbacks from GoogleApiClient) are in there (either by implementing the respective interface or by creating a private class). But I would rather put those into separate classes.
But the question that I ask myself is this: how would I then be able to access the class attributes of the activity class? Sure, I would then probably create setter/getter, but still I first need a reference to the Activity object. How would I get this?
Thanks and regards!
It's a really wide question, since the answer depends by your project and by your programming style. The first suggestion is: move what you can move in one or more fragment. All stuffs related to google play services can be nicely handled in a fragment for example. Listener and callback are UI related components, so they need a Context of an Activity to work, but you can split your UI (again) with Fragment and keep a piece of logic in a Fragment and another piece somewhere else. If you have some logic that runs in background, then you should consider using Service. I tend to have empty Activities, but this is not a rule.

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.

Android activity recognition and location client

I want to use an ActivityRecognitionClient and a LocationClient in the same Activity. The trouble is that either calls onConnected.
I would like to use both e.g. to write the location and the recognized activity to a database.
I thought that should be a rather easy thing to do but could not find out how. Hope someone can help.
UPDATE
I use Services now instead of Activities and implement the clients separately, connecting when the service is started and disconnecting when it is destroyed.
The same activity cannot implement two methods with the same name from different interfaces.
This is well explained here Java - Method name collision in interface implementation
You might want to make separate classes implementing each interface separately and call your methods to read/write to your database from both of them.

Categories

Resources