I have the DeskClock app on my phone and I was wondering if I can make the DigitalAppWidgetProvider that extends AppWidgetProvider update the view right from my system service. I tried sending a broadcast to ACTION_APPWIDGET_UPDATE, but it didn't work. Is this even possible?
Thank you in advance!
Related
I'm new to android and trying to build a simple app which needs to listen for incoming sms. I know that I need to use the BroadcastReceiver class and I also know how to make my own broadcast receiver. But how do I start it? Does it start automatically if I set the code for it in the manifest? The app just has the Main activity, do I need to somehow add a broadcast receiver in the onCreate of this activity? I searched for an answer, but it's still not clear to me. I know it's not nice to ask, but it would be great if you can share some sample code. Thanks!
If you declare the receiver within your AndroidManifest.xml, then you shouldn't need to do anything more. When a broadcast gets sent, the Android system will look through all installed apps and notify each app that has declared the appropriate Receiver in its manifest, starting the app in the process if necessary. For most cases, such as SMS, that is how you want to declare receivers, because most broadcasts are sent with the intent that you want to open your app when its not currently running to react to the broadcast.
Alternatively, you may declare the broadcast within a running activity, which may be useful if you want the broadcast to directly update the UI in your running app.
BroadcastReceiver Documentation
In the commom way, we receive events invoked by widget in AppWidgetProvider which is a BroadcastReceiver.But some Android Device can't not receive broadcast in AppWidgetProvider when the app is kill.
Any kind people can offer me some help?
Now I know how to fix it.
In AppWidgetProvider, I use PendingIntent.getService instead of PendingIntent.getBroadcast to bind event to RemoteView. And I handle the event from widget in a service.
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 :)
My app is for tracking, have to send to php all I got. I have methods such as postData to php, my location listener class and getMyPhoneNumber. My question is what should I choose to develop that app? I have read Android Service and Broadcast Receiver documents and I still can't figure it out. Which one of them is more suitable?
PS, Thanks in advance
suitable depends upon requirment .
if you want to receive particular broadcast or in other words want to do some operation on specific event like location change use broadcase Reciever while if you want something to happen consistently use srvice .
service will always run (in background) while BroadCastReceiver will receive related broadcast and run
I have an app which consists of a simple GUI and a few Broadcast receivers.
I found somewhere on here how to start an app on boot of the device but cant seem to locate it atm.
However i need more i need it to start in the background and just listen using the broadcast receivers and then return to this state after the Gui(the visible part of the app) has been used.
Is there a way of doing this?
Thanks.
Look into the Service class. Services run in the background and have no direct user interaction.
http://developer.android.com/reference/android/app/Service.html