Start foreground camera recording service in background - android

I'm developing app for enterprise which will be used in custom hardware setup without a display and Android 11. Now I'm debugging an app on a smartphone with a display (smartphone will be used as a platform in future but without a display). Android has a restriction (https://developer.android.com/guide/components/foreground-services#access-restrictions) for starting foreground services which are using camera or microphone in background, so I'm stuck here.
Is there any way to start this service without any user interaction? Maybe Device Policy Controller provides a way to remove this limitations?
Thank you.
P.S. App provides life safety features, user knows about filming and accepts it directly, also app informs user with text-to-speech announcement, so ethic side is ok here.

Is there any way to start this service without any user interaction? Maybe Device Policy Controller provides a way to remove this limitations?
Yes. You can start your foreground service without user interaction. You can schedule your foreground service to start at a certain time using AlarmManager or periodic time using WorkManager or start it remotely for example using FCM or even start it based on some events using BroadCastReceiver.
To start a foreground service in android O and above you must start showing a notification and specify foreground service types in manifest and at runtime. for example, in work manager, it will be like this.
To record video from foreground service, you can use Camera or Camera2 depending on your target android version. There is also CameraX which I did not use in the foreground service.
To start your app when startup you can use BOOT_COMPLETED broadCastReceiver. I hope this be helpful🙂.

Related

Open camera from foreground service which was started in background on Android 11

Based on https://developer.android.com/guide/components/foreground-services#bg-access-restriction-exemptions we can only start using camera in foreground service without launching app (making it visible) if:
The service is started by a system component.
The service is started by interacting with app widgets.
The service is started by interacting with a notification.
The service is started as a PendingIntent that is sent from a different, visible app.
The service is started by an app that is a device policy controller that is running in device owner mode.
The service is started by an app which provides the VoiceInteractionService.
The service is started by an app that has the START_ACTIVITIES_FROM_BACKGROUND privileged permission.
In my case I need to open camera in background without user interaction and when screen of device is off, in such case as I understand it's possible only with:
The service is started by a system component.
The service is started by an app that is a device policy controller that is running in device owner mode.
The service is started by an app that has the START_ACTIVITIES_FROM_BACKGROUND privileged permission.
System component - though this one I guess also isn't not for developers, because it means a system app - system component?
And also what about START_ACTIVITIES_FROM_BACKGROUND, can a regular application have this permission, how to grant it? Can't find any info about this one on Android site
If it's also only for system apps then the only choice is device policy controller to open camera from foreground service which is started in background on Android 11+?
p.s. if anyone is interested why such functionality may be needed. It's needed for dashboard camera apps. Users have a smartphone in their cars and when they for example turn on the phone (if it wasn't powered) they want video recording to be started automatically without their interaction.
So it would start recording automatically in all cases like a real dashcam device
But Android 11 brings these limitations...
This is no longer seems possible in Android 11 and up (for very good reasons!). What is possible is as soon as the device connects to a carkit or car radio (via Bluetooth, you can register a BroadcastReceiver that listens to intents with action ACTION_ACL_CONNECTED), you post a local notification. The user can launch the foreground service then by interacting with the notification.

How to create background services for android Oreo without notifications like social networking applications Facebook, Messenger, Zalo

I see some applications like Messenger, Zalo do not need notifications but still work in the background. How to do it. The devices I use are Xiaomi A1 (Android 8) and Nokia 6.1 (Android 8.1). Any solution guide is highly appreciated
Starting with Android 6.0 (API level 23), Android introduces two power-saving features that extend battery life for users: DOZE and APP STANDBY. These two features enforce many restrictions on your background processing while the phone is in Doze mode. You should read about Doze and app standby in the following link
https://developer.android.com/training/monitoring-device-state/doze-standby
Now, about your use case is that you want to receive the messages and incoming calls even when the app is not running. For this use case, Android announced High Priority FCM messages in GoogleIO2016. They are high priority Push message which grant the application temporary wakelock and network access, independent of Device's Doze state or if the app happens to be in the app standby. This allows the application to react to the message and notify the user in whatever way it wants about the instant message or incoming call.
For more about your use case, follow the below GoogleIO2016 Video from 08:30m to 10:30m
https://www.youtube.com/watch?v=VC2Hlb22mZM&t=505s
and read about this use case on the first link in this answer.
You can using Service class.
Refer this link, which I found on so: How to make an android app to always run in background?
Fix me if I have any wrong!
Hope it can help you.
No, you cannot start a service to work in the background when app is not in the foreground without showing the foreground notification API >= Oreo. I suggest you use WorkManager, a convenient lib for background tasks without foreground notification. Check it out there: Work manager
No. For API levels of Oreo (i.e.: 26) or higher, you cannot start a service to work in the background when the app is not in the foreground without showing the foreground notification. One alternative is to use WorkManager, a convenient lib for background tasks that doesn't require a foreground notification. Check it out here: WorkManager
The caveat with WorkManager though is that it does not allow you to execute tasks more frequently than once every 15 minutes. So if you need some background task to run every 30 seconds, 5 minutes, etc., you'll need to explore other options instead.

Foreground service android wear

I didn't find any documentation about that. Is it possible to create a foreground service on android wear? Usually the foreground state is related with an ongoing notification but it's not possible on wear so I'm a bit confused. Is there anyone has a foreground service on wear?
As per documentation:
"The system enforces a timeout period. If you are displaying an activity and user's don't interact with it, the device sleeps. When it wakes back up, the Wear home screen is displayed instead of your activity. If you need to show something persistent, create a notification in the context stream instead."
So, i believe it's possible to have a persistent UI (may be not an ongoing foreground notification but a card or a stack instead).
Hope it helps.

How do Android applications such as Facebook Messenger and Llama stay running in the background from boot time?

I'm developing an application that uses a Bound Service to query information from a server and provide notifications when conditions are met. At the moment, the user must execute the application from their home screen in order to begin receiving updates. But, for example, applications like the Facebook Messenger and Llama run from the moment the phone starts in the background. How do I achieve similar functionality for my long-term application? Also, even when my application is run from the home screen, it will still ocationally quit in the background from what I assume to be the system quitting the application for additional resources. Even though my application is made to restore the service when it begins again, it never seems to restart after it quits (usually after 3 to 4 hours of background activity).
Thanks for your help.
You can register a BroadcastReceiver for the ACTION_BOOT_COMPLETED Intent to detect when the device is booted. This requires the RECEIVE_BOOT_COMPLETED permission.
Instead of using a bound Service you can use a started sticky Service. However, depending on what exactly you want to do, you might want to check if AlarmManager suits your requirements better (maybe in combination with an IntentService, cf. cwac-wakeful).

Can I prevent the user from stopping my Android application?

The requirement is for an enterprise application. The application will be started on device boot. It will be running in the background and the user should not be able to disable or Stop the application. In Android a user can go to Settings->Application->Manage Application and stop my application. Is there any way to prevent this from happening?
No there is not. You can prevent Android from stopping the application by utilizing a Service and marking it as a foreground service, though this will require your application to display an icon in the status bar.
You can not make your application live forever, but it depends on what you really want to do. It's possible to receive a lot of events of the mobile and execute code even if your Activity/Service is not running. You can use BroadcastReceivers to look for interesting events and then start a service. I do it for an Enterprise Application that sends an event to a main server when the user has received/made a call.

Categories

Resources