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...).
Related
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.
If android application is installed in mobile but never launched and considered i have implemented boot completed receiver for this application.Now i am going to reboot the device .here boot completed receiver will call or what will happens?
No You have to launch the app once to register receiver with the device. Without launching the application Device will not detect your broadcast receiver.
Set the RECEIVE_BOOT_COMPLETED permission in your application's manifest. This allows your app to receive the ACTION_BOOT_COMPLETED that is broadcast after the system finishes booting (this only works if the app has already been launched by the user at least once)
The documentation clearly states that it works only if the app has been launched at least once. So the answer of your question is NO, it wont be called.
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).
I wrote program for broadcast receiver and service, but i confused in the manifest file there is some ground work to register service and receiver, will any one give me clear idea about this? Thanks in advance.
Service
It is used when you want to do something in background, any long running process can be done using Service in Background.
This will be running always in background even if the application closed
For example, you want to play music when your application gets close. In that case service will be running in background with music.
BroadcastReceiver
It is used when you want to fire some stuff or code during some event. For example, event can be on Boot of Device.
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
for example, If you want to perform something when device Boots, date and time changed etc.
A service is used to perform long running operations without user interaction or to supply functionality to other applications.
A Service needs to be declared in the AndroidManifest.xml via
a <service android:name="yourclasss"> </service> and the implementing class
must extend the Service class or one of its subclasses.
To start Services automatically after the Android system starts you can register
a BroadcastReceiver to the Android android.intent.action.BOOT_COMPLETED system
event. This requires the android.permission.RECEIVE_BOOT_COMPLETED permission.
For more details, check this http://www.vogella.com/articles/AndroidServices/article.html#pre_broadcastreceiver
A broadcast receiver is an Android component which allows to register for system or application events. All registered receivers for an event will be notified by Android once this event happens.
A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.
It'll be very long to explain it all here
I've got 2 great tutorial links from vogella
Broadcast Receiver
Service
if you have further question after reading the tutorial feel free to ask me in the comment :)
can i register a BroadcastReceiver from my activity but keep it active while the app is not running? (obviously if the user sets that wants this on my app settings)
This way i think to reduce cpu load if user decide of deactivate my app without uninstall it, it's possible?
Thank's
Valerio
Hi, can i register a BroadcastReceiver from my activity but keep it active while the app is not running? (obviously if the user sets that wants this on my app settings)
No. However, you can put a BroadcastReceiver in your manifest, which will allow it to get control when the rest of your app is not running. If you want the user to be able to enable or disable this, you can use PackageManager to enable or disable your receiver.