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?
Related
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 do I listen to incoming/outgoing calls particularly these 👇 implicit broadcast
android.intent.action.NEW_OUTGOING_CALL
android.intent.action.PHONE_STATE
In =>Oreo android system, these are not allowed to be consumed in the background even though both of them are allowed(i.e. not in exception broadcasts list)
I've tried:
Registering both of the broadcasts in manifest.xml and giving runtime permission then works only while the app is in the foreground
Had a foreground service that context registers these broadcasts, this solution works fine but shows a constant notification, I don't want that
Can anyone describe how TrueCaller or any such app is able to do so without notification?
Looking for solution which works even in if app is not being used
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.
How do I make a broadcast receiver which runs always, from the very start of the device? Is this possible without starting the application where it is declared?
If not I guess I would have to start my application when the device starts. But this probably adds to much overhead and it's unwanted.
I want this always runnning broadcast receiver in order to listen always for c2dm notifications. These should notify the user that there are new messages in the application.
If you add the BroadcastReceiver to your Manifest with an Intent Filter to listen for a specific intent, the Receiver will be active upon install.
what do you mean "run always" ?
if you need something to be alive for a long time , and from the OS bootup , you need to :
let the app to be installed only on the internal storage (otherwise it won't work).
set the broadcast receiver to listen to the boot intent .
upon receiving the boot intent, start a service and there listen to the intent you wish to listen to.
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.