I know that there is a system broadcast called android.intent.action.SCREEN_OFF, but I want to receive broadcast when user lock his phone not screen off.
I am developing an application that can tell user how long his phones is locked.
I don't see the broadcast you're asking for in the list of available broadcasts listed here https://developer.android.com/about/versions/11/reference/broadcast-intents-30). However, you could use the ACTION_SCREEN_OFF broadcast, and in its onReceive(), use one of the KeyguardManager class's methods to check if the device is locked. The method used would depend on your API version. For example, isKeyguardSecure() supports API 16 and above.
Related
I'm trying to detect the unlock of the phone with the app in background.
I'm registering a receiver with registerReceiver method to detect the unlock of the phone, and apparently it only works as long as the context used is alive so I used the application context :
getApplicationContext().registerReceiver(receiver, filter);
I want to know how long I can expect this receiver to work when the user leave my application and does not use it (so when is the application context killed).
I firstly wanted to use the ACTION_USER_PRESENT in the manifest but this need for read_phone_state permission (and it would be difficult to explain to the user why the app need to make calls...).
Now that the final API for Android O is released and none of the following broadcasts is whitelisted I have the following problem:
In my application (targets API 25) I currently have a BroadcastReceiver which listens for system events of ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED. Now I would like to update my app to target Android O but with this release comes a huge change in broadcast behaviors:
Apps that target Android O can no longer register broadcast receivers
for implicit broadcasts in their manifest. An implicit broadcast is a
broadcast that does not target that app specifically.
Since both broadcasts are implicit I can only register for them via the Context.registerReceiver() method but with this comes the problem: As soon as the process of my app is killed by the system or as soon as system clears my app's memory (as a result of low-memory of device) the broadcast registration will be lost.
To avoid this problem I can use the JobScheduler API with the setRequiresCharging method for ACTION_POWER_CONNECTED but for ACTION_POWER_DISCONNECTED I have to use the registerReceiver method.
Since my app controls the volume of the device (based on these events) it's really important that none of these events is missed. So how can I safely listen for disconnected power events in Android O?
Btw. I have the same problem with WIFI disconnect events.
EDIT: I would love to do this without a notification from a foreground service
Jobs can be delayed without any constraint by the system. The setOverrideDeadline method uses just a best-effort policy, so in this case you can keep your target SDK to 25 or use a foreground service. Foregournd services can be killed by the system but with really low probability.
This may be overkill, but you could register for those broadcasts in a foreground Service that is started by ACTION_BOOT_COMPLETED.
You can register a background job with the JobScheduler using the "requires charging" constraint. At least this is the official solution suggestion.
I know that it is possible to handle incoming sms on android and I think even me as a beginner can do that. But my question is: Will the app also run when the device is locked? I am working on an application that sends an email with the text and sender to a specific email address when the device received a SMS. But it also has to work when the device locked itself after a few minutes? Whats the best way to do that or is it already working by using the onRecieve method?
Thanks for any kinda help and please be kind I am quite new to programming :D
It's complicated...
As soon as an app is paused (that means : not displayed on the screen), it could be destroyed by the Android system to preserve battery or reduce CPU / RAM usage.
So : no, you have no guarantees the app will still be alive.
You can set a BroadcastReceiver to your AndroidManifest.xml and create a BroadcastReceiver class in your app. The onReceive() method will be called and the code you set in your class will be executed. Even if the app is not running at the moment the SMS is received.
But there's another issue : Deep Sleeping. To preserve battery, Android will turn off any battery-intensive systems when the device is not used for many hours. Battery-intensive systems includes : Wi-fi and Data. SMS is excluded from this list (but some constructors may include an option to disable SMS receptions when in Deep Sleeping, in this case, you have no option, just warn the user to not disable SMS receptions in Deep sleeping), gracefully.
That means implementing the onReceive() method will not be sufficient. You will need to wake up the device to enable Wifi and Data, allowing you to send an email.
So, to avoid this problem, extends a WakefulBroadcastReceiver. This is like a "normal" broadcast receiver, but it will wake up the device, and let it sleep again when the code is fully executed.
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.
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 .