Android equivalent of Swift's HKObserverQuery? - android

Hi I'm want to collect user health data. In iOS we have HKObserverQuery to observe.So when ever there is a change in health data for example, change in step count it will wake our app. Is there any thing similar in android for live updates. So I can setup monitoring for any changes.
I been scratching my head for few days. Thank you.

Broadcasts
Android apps can send or receive broadcast messages from the Android system and other Android apps, similar to the publish-subscribe design pattern. These broadcasts are sent when an event of interest occurs. For example, the Android system sends broadcasts when various system events occur, such as when the system boots up or the device starts charging. Apps can also send custom broadcasts, for example, to notify other apps of something that they might be interested in (for example, some new data has been downloaded).
You Could Use LocalBroadcastManager for your App when data changed it will called from your code
Then Register Reciever in manifest
send Broadcast locally fron your app to Broadcast Reciever when data changed in store Like HKObserverQuery
https://developer.android.com/guide/components/broadcasts.html

Yes, there is a CompletableFuture class in java to do so. You can read about it in Java's documentation

Related

Is it possible that one android app sends clicks to another open android app?

I´d like to control an app with another app.
As far as I know apps in background get paused.
Is there any way to send commands/clicks etc to another app by my control app?
Thanks
Not generally. If you wrote both apps, you are welcome to implement your own control IPC mechanism. An accessibility service can do what you want to a limited extent for arbitrary apps, but nobody with any sense will install your accessibility service, given that you can do all sorts of nasty things to the user and so there are security warnings that get raised when the user goes to activate your accessibility service. On rooted devices, there are probably many more options.
If you want to send click events to another App, you can achieve it by Broadcast Receiver.
You have to send a broadcast message and the other app must have a receiver to receive the trigger.
you can get more information about broadcast receiver by this link https://developer.android.com/guide/components/broadcasts.html
What you are looking for is a service. In a bounded service, Inter process communication is extremely easy, read here.
https://developer.android.com/guide/components/bound-services.html
You simply bound both your apps to a service they can talk to eachother.

Is it possible to trigger a notification in another android phone using broadcastreceiver?

I'm new in android development, and need some help and guidance in triggering my notification in my application. I manage to read some documentation about BroadcastReceiver where in it broadcast an announcement and another app can receive that announcement and trigger something to happen(like popping out a notification). I tried doing it, wherein I have 2 (two) application in 1 (one) android device and it worked. But when I try separate the 2 (two) application and install it separately into 2 (two) android device and try to send the broadcast, the receiver didn't manage to receive the broadcast. Then I tried to do more research and saw that BroadcastReceiver is a "System-Wide" broadcast, means (correct me if I'm wrong) it will only work inside 1 (one) android device. So my question is now, is there anyway to make the BroadcastReceiver send the broadcast in the network so that if an android device with a receiver can receive the sent broadcast? if it's not possible, is there another way to trigger notification in 1 device using another device w/o using the FCM/GCM?
PS:
The reason why I don't want to use FCM/GCM is that I'm trying to do this in an adhoc network, wherein internet connection is not present. And I'm not sure how FCM/GCM will behave w/o the internet connection. I appreciate any help. Thanks you.
You can setup a server in a background service on one of the devices and send data to if from the other device, and then have that service create a notification when it gets data.
If you want the devices to automatically be able to discover each other try using the network discovery service. https://developer.android.com/training/connect-devices-wirelessly/nsd.html

Android - Can you have multiple apps publishing the same intent?

I need to implement an android broadcast style IPC - not dissimilar in concept to UDP or a message bus.
Several android apps need to be able to broadcast messages "MessageTypeX" to listening android apps.
Thus there may be 1 or more applications that can generate messages of "MessageTypeX" and one or more applications interested in hearing about every "MessageTypeX" message. These consumers will all do their own things with the received message.
Can this be done - I'm anticipating it should be done using intents but I'm not quite sure?
Thanks.
Short answer:
Yes, you can register multiple BroadcastReceivers to the same intent. And ofcourse send them same.

Android: Infinitely running Service for receiving events?

I have a requirement that my app, can receive events (e.g. messages) from server any time (even if app is not running). So do I need to create an infinitely running service to listen for these events?
This to me seems similar to Email apps in Smartphones, like whenever you receive a new email(event in my case), its able to show notifications and also able to update my list adapter whenever I receive an event.
But I dun know how will I implement this?
You should take a look at C2DM (push-messages):
http://code.google.com/intl/sv-SE/android/c2dm/index.html#intro
it allows a server to send messages to devices at any time.
From the link:
Here are the primary characteristics of Android Cloud to Device
Messaging (C2DM):
It allows third-party application servers to send lightweight messages to their Android applications. The messaging service is not
designed for sending a lot of user content via the messages. Rather,
it should be used to tell the application that there is new data on
the server, so that the application can fetch it.
[...]
An application on an Android device doesn’t need to be running to
receive messages. The system will wake up the application via Intent
broadcast when the the message arrives, as long as the application is
set up with the proper broadcast receiver and permissions.
[...]
Events from server are called "push notifications" and are implemented via "Cloud 2 device messaging" (C2DM). On the mobile side these messages are submitted as broadcast events (see BroadcastReceiver). For a complete example see some tutorials: Google, Vogella or here on Stackoverflow
I think you have to start a Service as soon as the device booted. There is a good tutorial here how to achieve this.

What is the use of broadcast receiver in Android?

Can anybody tell what is the use of a BroadcastReceiver and give an example in Android?
Can someone give a time zone change example using a BroadcastReceiver?
I think the android developers documentation explains it pretty good:
A broadcast receiver is a component that does nothing but receive and react to broadcast announcements. Many broadcasts originate in system code — for example, announcements that the timezone has changed, that the battery is low, that a picture has been taken, or that the user changed a language preference. Applications can also initiate broadcasts — for example, to let other applications know that some data has been downloaded to the device and is available for them to use.
(see developer.android.com)
On your android system broadcasts are used as an instrument of notifications. As the name suggests, they are broadcastet through your whole system. With a broadcast receiver you can catch these broadcast notifications.
Think about the broadcast receiver as a normal listener. If you listening for free beer and someone yells "free beer here", than you will react on that :) Thats your real life broadcast receiver example :D
A broadcast receiver is a component which allows us to register for system or application events. All registered receivers for an event will be notified by Android once this event happens.
Android system periodically broadcast messages about things that are happening, such as the battery status changed, the Wi-Fi came on, or the phone’s orientation changed. You can pick up these state changes and perform actions after intercepting them and all this is done using broadcast receivers. Broadcast receivers receive and react to broadcasts generated by system or apps .

Categories

Resources