I am developing a widget and I need to detect a screen on or user present intent in order to update the widget when the screen is switched on.
I have checked similar questions and answers on stack overflow and came to the conclusion that registering a service that will listen to SCREEN_ON and SCREEN_OFF intents is not really a good solution since the service will be kept active all the time.
A possible solution is to detect screen on using the USER_PRESENT intent (which can be registered in the manifest and delivered to the widget, hence no need to keep a service in memory) and then update the widget as necessary. You can also register the screen on/off service on USER_PRESENT to handle the SCREEN_OFF intent and unregister it on screen off).
The problem is that, from previous experience, the USER_PRESENT intent is not always broadcasted. There are cases where the user does not have any screen lock defined (i.e., he selects the swipe method) and in such cases, the USER_PRESENT intent is not broadcasted.
Is there a way to make sure that the USER_PRESENT intent is broadcasted on all devices? If not, is there another way to detect when the screen turns on?
Related
I want to check when a user Unlocks his device, and run a piece of code. How can I know If user unlocked?
I tried creating a broadcast receiver, registering it in the manifest with an intent filter for action USER_PRESENT. But from android Oreo restrictions were imposed on broadcasts.
I've tried an implicit broadcast receiver, but its life ends with the app getting killed.
"How to get unlock or USER_PRESENT event even after the app is killed"
can a background service be used here?
or any other broadcast?
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.
In particular, I am interested in intents generated as a result of a hardware button press.
If the sender is using sendStickyBroadcast() or sendStickyOrderedBroadcast(), then the broadcast is sticky.
I will be somewhat surprised if hardware button presses are sticky broadcasts, simply because a button press is truly a point-in-time event. Sticky broadcasts are typically used in cases where the last-broadcast-value is of relevance. So, for example, the current battery level is relevant, so ACTION_BATTERY_CHANGED is sticky. But if, say, ACTION_CAMERA_BUTTON were sticky, then all we would find out is whether or not the CAMERA button had been pressed at any time since the phone was last rebooted, which is not usually relevant.
My application relies on the intent ACTION_USER_PRESENT being fired, and so I set up a receiver in the manifest and I have a class that starts a service when it receives the intent.
However, when a user is using a lock-screen replacement app like WidgetLocker, the ACTION_USER_PRESENT intent may never be sent, or can be sent a bunch of times. (Once it was sent 5 times...) WidgetLocker's website explains that the application does send its own intent for unlocking, com.teslacoilsw.widgetlocker.intent.UNLOCKED. In certain configurations of WidgetLocker, ACTION_USER_PRESENT may be fired before the user even unlocks the screen, so I was told that it would be best to set up a check for com.teslacoilsw.widgetlocker.intent.LOCKED, and then wait to receive the UNLOCKED intent and do my work.
My problem is that I'm not sure how to set up a receiver for a third party intent. I've added the actions to my receiver in the manifest just find, and I know that my broadcast receiver picks them up, but I need to filter them out. Mainly, if I pick up the LOCKED intent, I want to ignore any ACTION_USER_PRESENT intents, and instead wait for the UNLOCKED intent, but I don't know how to wait for an intent upon receiving a different one.
Mainly, if I pick up the LOCKED intent, I want to ignore any ACTION_USER_PRESENT intents, and instead wait for the UNLOCKED intent, but I don't know how to wait for an intent upon receiving a different one.
Step #1: Create separate BroadcastReceivers for the WidgetLocker actions vs. ACTION_USER_PRESENT.
Step #2: Upon receipt of LOCKED, use PackageManager and setComponentEnabledSetting() to disable your ACTION_USER_PRESENT receiver.
Step #3: Upon receipt of UNLOCKED, use PackageManager and setComponentEnabledSetting() to re-enable your ACTION_USER_PRESENT receiver.
This could get a bit dicey in edge cases (e.g., user pops out the battery while LOCKED), but it's a starting point.
What is a BroadcastReceiver? What are its uses and how can I use it?
Start by reading the documentation. Also, copying from Application Fundamentals:
Broadcast receivers
A broadcast receiver is a component that responds to system-wide
broadcast announcements. Many
broadcasts originate from the
system—for example, a broadcast
announcing that the screen has turned
off, the battery is low, or a picture
was captured. 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.
Although broadcast receivers don't
display a user interface, they may
create a status bar notification to
alert the user when a broadcast event
occurs. More commonly, though, a
broadcast receiver is just a "gateway"
to other components and is intended to
do a very minimal amount of work. For
instance, it might initiate a service
to perform some work based on the
event.
A broadcast receiver is implemented as a subclass of
BroadcastReceiver and each broadcast
is delivered as an Intent object. For
more information, see the
BroadcastReceiver class.
Finally, read in Common Tasks how you can utilize BroadcastReceivers to listen for messages and set alarms.
A broadcast is generated by android on occurrence of some action , BroadcastReceiver class enables the developer to handle the situation on occurence of the event/action . Action can be arrival of msg or call , download complete , boot completed , etc.
Broadcast Receivers simply respond to broadcast messages from other applications or from the system itself. These messages are sometime called events or intents. For example, applications can also initiate broadcasts to let other applications know that some data has been downloaded to the device and is available for them to use, so this is broadcast receiver who will intercept this communication and will initiate appropriate action.
I like this slide, because it focuses on Broadcast Receiver and offers simple description. The minor problem is that the updated date was a little bit old ( in 2011 ).
Link
Android Application Component: BroadcastReceiver Tutorial
(retrieved from the slide)
Broadcast Receiver
Receives and Reacts to broadcast Intents
No UI but can start an Activity
Extends the BroadcastReceiver Base Class
BroadCastReciever is an Android Component that helps you to know handle registered System Events or Application Events.
For Example:
System Events Such us : the screen has turned off, the battery is low, or a picture was captured.
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... etc
In simple terms
A broadcast receiver is basically an interface that you can implement so that your app can subscribe to system changes like when the system has finished booting, or a charger is connected/disconnected or airplane mode is switched on/off etc.