I want my application to start when someone modifies a content provider. A setting to be specific. The settings framework calls "notify" when a value is set.
If my app was started I would use registerContentObserver() I guess, but is is not started.
Can define some intent-filter in my manifest that wakes up my application. A back up plan would be to have a service running all the time that has registered a listener, but that seems like a wast or resources.
Thanks, Ola
This isn't directly supported by the Android device because starting an app every time a ContentProvider's data changes is a path to really killing your battery. To do the query, you'd need to do it in a service, which as you said is understandably undesirable.
Secondly, starting an intent is a user action. Android really doesn't support allowing an application to start all on its own without user request... Doing so would be impolite! What if your user was doing something important and then your app pops up on top? Remember the user is in control, not you. Instead of starting an application, consider placing a Status Bar Notification so the user can deal with it when it's convenient for them.
Related
I am developing an application for a business entity. That application should run in the background in every employees' mobile phone. Employees are mostly salesman. The application basically detects location changes and suggest the salesman where they might visit. A kind of reminder application. It also lets other salesmen see where are their teammates.
Right now I am using a foreground activity and it works fine till the system forcefully doesn't kill the service or the phone doesn't reboot due to manual activity or battery discharge.
Ones the application is closed, as of now, the managers in the firm needs to call salespeople to turn on the application once, as on application start it automatically turn on its foreground service. But this is really an extra burden on the management team which can be automated.
I am ok to have any settings based or code based solution. One solution is to root the phones of salespeople and install some extra utility app or write the code based on root APIs, but this will be too much for this simple task.
THe permission RECEIVE_BOOT_COMPLETED was not added properly in the manifest. After adding the permission it worked calmly. In on receive method of the broadcast receiver, I am starting the foreground service.
At the moment, the best way is to use WorkManager https://developer.android.com/topic/libraries/architecture/workmanager/ Yes, it still alpha, but works very good.
From other side, you could work on automating the task "managers in the firm needs to call salespeople to turn on the application once". I mean, an app/backend could automatically call the salesman (with some pre-recorded message) or send SMS to them.
Note: I haven't coded anything but looking for a structure/keywords or ideas how it can be achieved.
Use Case:
I have an activity which basically shows some display/data/or does some operation etc when opened manually from button click, notification click etc. Now I want to display the same stuff when a particular condition is satisfied [like my geo location matches that I force to be my home or user home].
1. without user clicking any notification,
2. without opening the activity,
3. without clicking buttons, and
4. without opening the app.
So in short, I want my app to be launched when I am home or reach a geo fence, is it achievable?
I am aware of Pending Intents, but seems they need the activity to be opened once and register the intent, let me know if this is the only way to go.
Kindly help!
when a particular condition is satisfied
This is not possible in general. For example, Android has nothing built-in with the ability to do something automatically based upon when your neighbor's cousin's daughter posts something on Stack Overflow. Even though that is a particular condition that could be satisfied, Android does not know your neighbor, nor your neighbor's relatives, nor the neighbor's cousin's children, nor the online actions of your neighbor's cousin's daughter.
There are specific things that Android can handle that offer the ability to give you control for related conditions, such as AlarmManager for getting control at certain points in time, or JobScheduler for getting control periodically when the environment is proper (e.g., we have connectivity and are on a charger).
Your question uses a geofence as an example. The Play Services SDK offers geofence APIs that you can use, that can give you control when the user enters a particular region.
I want my app to be launched when I am home or reach a geo fence, is it achievable?
You are welcome to call those geofence APIs, providing a PendingIntent that points to a WakefulBroadcastReceiver, which in turn points to an IntentService, where you can go do something.
If by "my app is launched", you mean bring up one of your activities, while that is technically possible using an activity PendingIntent, you may make your users very unhappy for interrupting what they are doing (e.g., collecting Pokémon). Please consider using a Notification instead.
I am aware of Pending Intents, but seems they need the activity to be opened once and register the intent
Nothing of your app will ever run until the user manually runs your app (e.g., taps on your home screen launcher icon) or until something else uses an explicit Intent to start one of your components. At that point, you can call the geofence APIs, assuming that you somehow know where the user's home is.
Most likely, you need the user to spend time in the activity simply to collect the data from the user about where they want the geofences to be established.
I have a device management application, which essentially runs as a service in the background from boot. I'd like to start this application immediately after installation. How do I achieve this?
You cannot do this -- there is no way to automatically start your service merely because it was installed.
The application must first be invoked by the user through some sort of activity. Or, you are going to need to hook into some relevant broadcast Intent via the manifest, so you can get control when one of those events occur and kick off your service that way. Or, you are going to need to ask the user to reboot so your BOOT_COMPLETED Intent filter can get control.
There was a hole - the Android Analytics SDK used to send an intent right after installation - but that got closed (producing lots of confusion, of course).
But the final answer, I believe, is here:
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
This seems to suggest that, as of 3.1, Google made the decision that apps are in a stopped state until the user explicitly activates them, e.g. by launching app or placing widget.
This means that the strategy of listening of a common broadcast (i.e. to get your app launched surreptitiously) won't work either.
After 3.1 , Android introduced a security feature, where the application's code can't be run unless the user opens the application. Check the link for more info http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html
I would like to know if anyone has found any hack or work around for this problem, where i can listen to a system broadcasts like boot, connectivity changed and run a service without opening the installed application.
This is not what the posts said. You can still register broadcast listeners, or alarms. You just need the user to actually start your application once. And, if the user forcefully stops your application, they'll need to start it manually again.
Why would you want a way around this behavior? It seems to make a lot of sense.
I have a device management application, which essentially runs as a service in the background from boot. I'd like to start this application immediately after installation. How do I achieve this?
You cannot do this -- there is no way to automatically start your service merely because it was installed.
The application must first be invoked by the user through some sort of activity. Or, you are going to need to hook into some relevant broadcast Intent via the manifest, so you can get control when one of those events occur and kick off your service that way. Or, you are going to need to ask the user to reboot so your BOOT_COMPLETED Intent filter can get control.
There was a hole - the Android Analytics SDK used to send an intent right after installation - but that got closed (producing lots of confusion, of course).
But the final answer, I believe, is here:
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
This seems to suggest that, as of 3.1, Google made the decision that apps are in a stopped state until the user explicitly activates them, e.g. by launching app or placing widget.
This means that the strategy of listening of a common broadcast (i.e. to get your app launched surreptitiously) won't work either.