static broadcast receiver above andorid N - android

I am creating a custom battery charger animation app.
I want to listen action ACTION_BATTERY_CHANGED using Broadcast Receiver. Broadcast will listen even if app is not on mobile stack.I do not want you use Foreground service as it always shows a notification and consume more battery.From android Oreo we can not declare all broadcast receiver on manifest.
In short a want to listen broadcast above android Oreo while my app in not on mobile stack and I do not want to use Foreground service.
Please suggest what should I do.

Related

Broadcast Receiver with action PHONE_STATE is not working after App gets killed

I want to get broadcast for incoming calls in my android device. and for this I have used the broadcast receiver with action PHONE_STATE & register it statically in AndroidManifest.xml as per below added screenshot :
This broadcast is working fine till my app is in foreground or active in recent apps. But It's not working after my app gets killed from recent apps.
and this broadcast action has exempted from limitation of broadcast mentioned in this link: https://developer.android.com/guide/components/broadcast-exceptions.html
I have tried by registering this broadcast from my Activity & also by registering it statically in AndroidManifest.xml file But still I am not able to get any broadcast after my app killed from recent apps.
I want to get broadcast every time for all the incoming calls even after my is not active or got killed from receiver.
Please give some solution to achieve this. or
Please suggest me any other solution or any other broadcast through which I can achieve this.

How to get broadcast ACTION_POWER_CONNECTED on Android 8

I am trying to make an app that shows some information when you connect your phone to a charger during a specific period of the day.
Before Android Oreo this was easy, just add an Broadcast receiver and add android.intent.action.ACTION_POWER_CONNECTED to the intentfilter in the manifest.
Now I am struggling to find a working solution to create the same functionality.
I thought about periodically checking the device charging state but this feels wrong and it won't trigger immediately on connecting the device to a charger.
Registering the broadcast receiver from my application did not work either, when the application is closed the broadcast isn't received anymore.
Is there a battery friendly way to trigger an action when an Android device is connected to a charger in Android Oreo?
There are broadcasts which are exempted from the background execution limitations and for which broadcast receivers can still be registered in the manifest.
You could use the ACTION_BOOT_COMPLETED broadcast to start a service which registers a receiver for your ACTION_POWER_CONNECTED broadcast at runtime. (The service must be a foreground service. Otherwise, it may be destroyed.)
A other solution would be to use JobScheduler to create a job which requires charging. Then you don't need the foreground service.

Android app which only have a service starts in the background can't receive broadcast message

I'm having a problem about android BroadcastReceiver. It's the first time for asking questions on stack overflow, and my English is bad. So I describe the question in Chinese again in the end!
I want my app can automatic run when the android system starts.First I register a BroadcasetReceiver which can receive the BOOT_COMPLETED message in the static way (in the AndroidManifest.xml).This BroadcastReceiver works well. The BroadcastReceiver needs to start a service, the service is running all the time, and it registers another BroadcastReceiver which can receive the CONNECTIVITY_ACTION message in the dynamic way.But when the networks changes, the app can't receive the CONNECTIVITY_ACTION message in the HUAWEI pad only when the app starts the main Activity.(It can works well in the HUAWEI phone).
note:The BroadcastReceiver which receives the CONNECTIVITY_ACTION message must be register in the dynamic way. Because the app need to unregister it sometimes.
(写了一个开机自动启动的程序,开机启动一个service,这个service一启动就动态的注册一个监听网络变化的broadcastreceiver,可是在华为的pad上,这个broadcastreceiver始终不能接收到网络状态变化的广播消息,除非启动主Activity,但是华为手机没有这个问题!)
1、Your phone's Android version maybe not same to your pad's;
2、I suggest you register broadcast in static way;
3、Are you sure this service started after reboot? you can write a log .

How to receive Broadcasts when app is off?

How can I make an app respond to ACTION_SCREEN_ON broadcasts even when the app is off?
I have been reading about alarms and IntentService but I am not sure what the best practice is.
I am trying to make an app that takes pictures whenever the screen is unlocked.
Register your BroadcastReceiver in a service.
From Broadcast Receiver within a Service

Difference between Service and Broadcast receivers in android

I want to know the difference between services and broadcast receivers, can anyone point out an example that can be observed on android mobile devices.
Thanks
Service: If you want to do something in background , this will be running always in background even if the application closed. You can create this in separate process and also you can give your service to other app if you want. Downloading any content or Music is good example
Broadcast Reciever: 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
Here is good article : Service and BroadcastReceiver
Service is used when you want to do something in background, any long running process can be done using Service in Background. For example, you want to play music when your application gets close. In that case service will be running in background with music.
Example of Service
BroadcastReceiver is used when you want to fire some stuff or code during some event. For example, event can be on Boot of Device. If you want to perform something when device Boots, date and time changed etc...
Example of BroadcastReceiver
I think of it possibly a different way. A Service receives intents that were sent specifically to your application, just like an Activity. A Broadcast Receiver receives intents that were broadcast system-wide to all apps installed on the device.
(The reason I say a Service is a bit like an Activity is that: You wouldn't broadcast a message saying "start Activity MyActivity" across all apps installed on the device. It is only for your specific app.)
Of course, as others mentioned, a Service can continue running in the background, whereas a Broadcast Receiver should finish quickly (e.g. if it is running for more than 5 seconds it may be killed by the OS). The Broadcast Receiver can still run in the background (when app is closed) under certain circumstances. For this, it's worth mentioning that there are actually two types of Broadcast Receivers - Manifest-declared, and Context-registered. They have different lifespans and restrictions - the former can receive broadcasts in the background with certain restrictions, while the latter cannot receive broadcasts in the background (app must be running and active) but has no restrictions on the types of intents that can be received.
Both services and broadcast receivers must be specifically invoked (via an intent), but for services this is usually a specific call (e.g. when your app is started or when the user clicks some button) whereas for broadcast receivers they don't need to be explicitly started as they will start anyway when a relevant broadcast is made.
Here's how I would think of it:
Type
Displays UI?
Can continue running for a long time when app is closed?
Can receive intents when app is closed?
Intents must specifically target your app?
Restricted list of intents that can be specified?
Activity
Yes
No
Yes
Yes
No
Service
No
Yes
Yes
Yes
No
Manifest-declared Broadcast Receiver
No
No
Yes
No
Yes1
Context-registered Broadcast Receiver
No
No
No
No
No
1: Only if you target Android 8.0 or above. The restrictions are not applied if the intent specifically targets your app. The restricted list of intents can be found here.

Categories

Resources