Activity Recognition when the app is not running? - android

In the demo video provided by google, it is said that you don't need to keep a service running to receive updates on activity recognition. But all code examples I could find show that you need to register ActivityRecognition in MainActivity for it to work.
How can I make Activity Recognition independent of application lifecycle?
For e.g. If a user is jogging, show him a notification to record his walk?
Can it work similar to a broadcast receiver which is called as soon a user is connected to wifi?

As an example you could subscribe to ActivityRecognition events when your phone booted successfully. You only need to do it once. After that the PendingIntent is called in the specified interval and you do not need to have a service running all the time in the background.

Related

Android: How to start activity when phone is sleeping, similar to Viber incomming call activity

I am developing an app and this app needs to give a clear indication to the user when some event happens.
Only thing I could do until now is giving a notification in the notification area. But, I need to give a more visible notification, similar to the behavior when phone is ringing in an incoming call.
As I can understand, the reason why android is only allowing apps to give a notification is to prevent apps from disturbing the user. But, this app I am developing plays a vital role in the job of the user, so I don't think it is inappropriate to give a such strong notification.
I know it should be doable since apps like Viber can start an activity similar to a incoming phone call, even when the device is sleeping.
Does anyone know how to get this done?
Register a broadcast receiver, and add a custom action to it say CustomAction.Instead of showing notification, throw a broadcast and add CustomAction via intent filter.
Now in the onReceive method of broadcast listener, check
if(intent.getAction.equals("CustomAction"))Intent i = new Intent(context, YourActivity);
context.startActivity(i);
Sorry for not a formatted answer, I'm driving, will update it later for more clarification.
Update
Register broadcast receiver in a sticky service. So that service can be started automatically if killed and register broadcast register again.
Don't forget to unregister broadcast receiver in onDestroy() method of service and also in YourActivity when you purpose is resolved.
Just adding a sticky service (which does nothing) fixed the issue. Adding the service prevented the process getting killed when user exits the app and removes it from recent app list.
Because of the service, the app process is running even when a no UI is visible. In this state, if an activity is shown from the GCM service, it gets shown.
You can trigger a broascast as Vinay mentioned. If it still does not work, try using wake-locks. These wake-locks help in waking the device when it is in sleep mode. It will act like force wake and after calling wake-locks, you can perform your actions.
Hope it helped..
Thanks.

What happens to background service when the app is updated in android?

Let's say I started a repeating background service that was stated on first app launch and on boot. What happens when I provide an update of the app. Will that background service be killed?
Will user have to open the app again to register the background service again or app will get some callback on update?
Edit-1: As one of the answer suggest if app has to be relaunched again to start the service then how does alarm application works fine after the update without relaunching(I believe it usages background service to start the alarm)?
Will that background service be killed?
It will be killed.
Will user have to open the app again to register the background service again or app will get some callback on update?
It depends. Basically it'd require user activity as app is not relaunched automatically after update. But if you target API 12 or higher (which you should nowadays) you can try to use ACTION_MY_PACKAGE_REPLACED broadcast. As per doc:
Broadcast Action: A new version of your application has been installed over an existing one. This is only sent to the application
that was replaced. It does not contain any additional data; to receive
it, just use an intent filter for this action.
so you can do you stuff either in BroadcastReceiver trigger something once you receive this broadcast.
The service will be killed and needs to started again.
A Service doesnt run on a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
So when the application is updated, the application is sent to the stopped state.
You can test this.
From google play store initiate a update for the app (which has a service E.g Whatsapp).
Open the app and wait for it to complete. It stops. you can check the internal running processes. Connect the phone to DDMS. Check the processes.

Service stops automatically in Android

I am working on a Android Project that control Android Device through Speech. For that I am using speech Recognition Service of Google.
Here I have created a service for handling Speech recognition.
But what is going is that after some time this service gets stopped automatically and my device stops responding to the speech of the user.
Then little seconds later, it starts responding again.
So my question here is that how can I stop my service from being destroyed automatically and if possible is there any trick to call my broadcast receiver whenever my service stops?
So that I can create my Broadcast receiver for handling service destruction.
And how can I also stop my device from ringing beep tune when Speak recognition Service going on?
You have to start your Service with startForeground(). You have to pass it a Notification that will be placed in the notification bar permanently. This make your Service unstoppable from the system.
As always refer to android documentation for more details.
http://developer.android.com/guide/components/services.html#Foreground

What is the difference between Broadcast Receiver and service?

I wrote program for broadcast receiver and service, but i confused in the manifest file there is some ground work to register service and receiver, will any one give me clear idea about this? Thanks in advance.
Service
It is used when you want to do something in background, any long running process can be done using Service in Background.
This will be running always in background even if the application closed
For example, you want to play music when your application gets close. In that case service will be running in background with music.
BroadcastReceiver
It is used when you want to fire some stuff or code during some event. For example, event can be on Boot of Device.
Usually system will send some info which can be recieved by your app if you would wish to ,by registering. And you can do something what you want when that thing happens by using onReceive method. Example is the system will send BroadcastReceiver when new sms arrives or Booting done
for example, If you want to perform something when device Boots, date and time changed etc.
A service is used to perform long running operations without user interaction or to supply functionality to other applications.
A Service needs to be declared in the AndroidManifest.xml via
a <service android:name="yourclasss"> </service> and the implementing class
must extend the Service class or one of its subclasses.
To start Services automatically after the Android system starts you can register
a BroadcastReceiver to the Android android.intent.action.BOOT_COMPLETED system
event. This requires the android.permission.RECEIVE_BOOT_COMPLETED permission.
For more details, check this http://www.vogella.com/articles/AndroidServices/article.html#pre_broadcastreceiver
A broadcast receiver is an Android component which allows to register for system or application events. All registered receivers for an event will be notified by Android once this event happens.
A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.
It'll be very long to explain it all here
I've got 2 great tutorial links from vogella
Broadcast Receiver
Service
if you have further question after reading the tutorial feel free to ask me in the comment :)

Launch app on incoming call (android)

I have an app on Android that reacts to incoming calls.
Now, since the OS shuts my app down whenever it want,
I need a way to to listen to the incoming calls and launch the app when it happens.
Will a BroadcastReceiver help? (just like launching on device restart)
Any idea?
thanks!
I think you have answered your own question. This is just the sort of thing a broadcast receiver is meant for. If the receiver is registered in your manifest then the application does not have to be running.
It will be automatically started when a matching intent is broadcast. Typically the response will be to update content or activities, make notifications with the Notification manager or launch/manipulate services.
Note that there is a 5 second execution limit in the BroadcastReceiver onReceive handler to ensure you do not try to do any 'heavy lifting' in it. Exceed this and a force close dialog will be displayed.
Yes a BroadcastReceiver would to the job as it will fire even you app is not running.

Categories

Resources