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
Related
In my app I used sharedpreferences to save my friends number.
When I need to message them it needs to open my app and then shake to use accelerometer sensor, then message goes.
Here I want to send messages without lunching my app by shaking my phone.
How I can do this?
Interesting question. You should try a broadcast receiver with appropriate filters or a background service. There may be a better approach, just my initial thought.
BroadcastReceiver
https://developer.android.com/reference/android/content/BroadcastReceiver.html
(if the action of sending takes too long, you may prefer to couple this with a wakeful broadcast receiver AND delegating the work to an Intent Service: https://developer.android.com/training/run-background-service/create-service.html)
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
This might be a foolish question for you, Please consider that im not an expert in Android programming.
I had implemented a PhonestateListener with an inline code in my Service.But after following the broadcast tutorial from here.I saw the same phoneStatelistener is explained with the help of a BroadcastReceiver.Can anyone pls explain me the benefits?Thanks.
A Broadcast receiver wakes your application up, the inline code works only when your application is running.
For example if you want your application to be notified of an incoming call, even if your app is not running, you use a broadcast receiver.
If your application is playing audio, and you want to stop the music on an incoming call, you use the inline code.
A broadcast receiver also called as receiver is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android run time once this event happens. Broadcast receiver is also called the gateway between your application and outside world.
example of broadcast receiver is when new sms arrives, broadcast receiver sends notification to messaging app and small icon pops up in notification bar.
rules of broadcast receiver:
it has maximum limit of 10secs,
do not do any asynchronous operations which may take more time,
do not do heavy database operations or networking operations in broadcast receiver.
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 :)
I am going to implement Download Queue for my application. One of its feature will be when Network get connected or disconnected. I want to notify my QueueManger(Service) to update items status and stop.
I know use of Broadcast Receiver and how to use it. My question is if my application is stooped (finished) will broadcast receiver will work? Will it notify application or not?
If not is there any way to achieve this functionality even when application is stooped.
If you register your receiver in the XML instead of programmatically, then it should be able to catch these even when the program is not running.
See question, same solution applies Intent action for network events in android sdk
If you have defined your broadcast receiver as <receiver> in AndroidManifest.xml and listening for android's default action strings, then Yes, it will work.
Otherwise, I would recommend you to make use of Service. It can run silently in the background and do all the logic you want. Your service can even be provoked automatically when the phone is restarted (if you code correctly).