I am using IntentService in my application. I want to know that, Is this Necessary to putExtras() before calling to startService(intentService) Method. Or can i call the startService(intentService) without providing data to intent. I want to know the reason behind this concept.
Thanks in advance.
Is this Necessary to putExtras() before calling to startService(intentService)??
Answer is No.
putExtras() is used to pass the data between Activitys and Services.if you want send some data to Service then Use putExtras()
The same intent here in IntenetService will received onStartCommand(Intent intent, int flags, int startId) get the data from the intent using getExtras() method
No. It is not needed. If you want to pass some values to the activity which is started by Intent then only you need to use putExtras(). Otherwise use can start activity using startActivity(intent) without implementing putExtras()
Related
I try to get the calling Intent from within a Service.
The method Service.getIntent() is deprecated and the method parseUri(String uri, int flags) is suggested as its substitute.
I do not know what to pass as the uri-string.
Where to get the URI of the Intent?
(I wonder why the method toUri() is suggested in the API to get the URI of an Intent you can not access yet...)
I try to get the calling Intent from within a Service
It is passed into onStartCommand() and/or onBind(). In some cases, it is passed along to you later (e.g., IntentService gives you the Intent in onHandleIntent(), based on its built-in implementation of onStartCommand()).
If you need the Intent outside of those methods, it is your job to hold onto it (e.g., as part of some "job" object that represents some work that the service is executing on a background thread).
This question already has an answer here:
Multiple activities binding to a service
(1 answer)
Closed 7 years ago.
I want to create this service to work with multiple activities. I mean each activity will be able to send a data to this service and get a data from it.
Basically, i would suggest that you start the service in application class and send broadcast to service whenever its needed to send any data to service.But make sure the service is running.
When you create a Service you should override the onStartCommand() method so if you closely look at the signature below, this is where you receive the intent object which is passed to it:
public int onStartCommand(Intent intent, int flags, int startId)
So from an activity you will create the intent object to start service and then you place your data inside the intent object for example you want to pass a userID from Activity to Service:
Intent serviceIntent = new Intent(YourService.class.getName())
serviceIntent.putExtra("UserID", "123456");
context.startService(serviceIntent);
When the service is started its onStartCommand() method will be called so in this method you can retrieve the value (UserID) from the intent object for example
public int onStartCommand (Intent intent, int flags, int startId){
String userID = intent.getStringExtra("UserID");
return START_STICKY;
}
Note: the above answer specifies to get an Intent with getIntent() method which is not correct in context of a service
In this scenario, you should consider using IntentService. IntentService is a special kind, which handles a queue of work, sent through Intents.
When the first activity calls startService(), the service is started and begins its work. Consequent calls to startService, will queue them and results in the works being processed one after another, until the last sent intent is done, and then service will shutdown it self. It's pretty straightforward to use and takes care of the heavy lifting and all the boilerplate code you should write.
For further study, you can take a look at this .
I would like to have your opinion.
I have got an Activity A with a button (and a listener of course). It starts a Service and a Notification.
I would like that when I click on the notification, it runs a new Activity B and it stops the Service.
My problem is : how use an Indent to send the datas from the Service to the Activity B when the Service is stop? (I need the very last values of datas in my Service)
Thanks in advance for yours answers.
Intent has a couple of methods called putExtra(String name, ...) which allow you to put a number of EXTRAS on the intent. You don't specify a whole lot of detail in your question. This is the most generic answer I can give you.
Before calling stopSelf() on the service you should start the Activity B with the intent (say I). Immediately after calling startActivity(B, I), you should call stopSelf on service.
While creating I, you can put data into the intent as EXTRAs.
I have a questions here. I wanted to communicate to my service from my broadcast receiver. So I used the StartSevice(intent) method. In the service side the OnStartCommand() is called everytime I called StartService(). I just need to know how can i recognize in the onStartCommand() from where it is called from?
I hope I am clear in my question. If not please let me clarify...
You can't really check where you're called from, but you can do something a little bit related. You can specify certain tags in the Intent that you pass to the onStartCommand(). Notice that onStartCommand() takes an intent as its argument, this will be the intent that you sent. Whenever you send an intent to start the service from your broadcast receiver, you can specify certain "extras" in your intent and then use get those in your service.
http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int)
By the way, the use case you give seems to line up more with an IntentService than a regular service. This is a service which does what you're doing already: it sits there and waits for intents to be fired at it, and then reacts accordingly:
http://developer.android.com/reference/android/app/IntentService.html
In the onStartCommand method, the Intent you used to start service is passed as an argument.
onStartCommand(Intent intent, int flags, int startId)
So you can add Extras to intent to distinct where from it is called.
intent.putExtra("From", "MainScreen");
I have a Service that can be stopped in multiple ways. Whenever I call stopService(Intent), I pass an intent with some extras. How do you retrieve those extras?
Thanks.
You need to override onStartCommand() in your Service this is how you get a reference to the incoming intent from startService.
In this case you would have a special action in your intent to tell the service to stop itself. You add extras to this intend which can be read in the onStartCommand() method.
Sample Code
public class MyService extends Service {
#Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
final String yourExtra = intent.getStringExtra(YOUR_EXTRA_ID);
// now you can e.g. call the stopSelf() method but have the extra information
}
}
Explanation
Every time you call context.startService(Intent) onStartCommand() will be called. If the service is already running a new service isn't created but onStartCommand is still called. This is how you can get a new intent with extras to a running service.
I found that the extras are not passed with the intent when stopService is called. As a workaround, simply call startService(Intent) and stopService(Intent) right after one another.
Example code from Activity:
Intent j = new Intent(SocketToYa.this, AccelerometerDataSocket.class);
j.putExtra("com.christophergrabowski.sockettoya.DestroyService", true);
startService(j);
stopService(j);
In Service.onStartCommand,
intent.hasExtra("com.christophergrabowski.sockettoya.DestroyService")
will evaluate to true, and you will destroy your service the way intended by the API (i.e., by calling stopService, which will in turn call OnDestroy after onStartCommand is called).
My suggetion is that use static member in class that extends Activity for passing information to service & it in service as normal static member access in outside class
Please don't do this unless you have no other option. You should try to use the mechanisms built into the framework for passing data, and not use public static fields unless there is no other choice. Read the Service documentation for examples.
Are you able to use an Intent with a "shutdown" action with Context.startService()?
That is, send an Intent with a shutdown action and extras to Service.onStartCommand(), decide how to shutdown based on the extras, then use Service.stopSelf() to stop the service.
I agree this isn't a great solution, since it potentially starts the service in order to shut it down. I would still like to hear of the "correct" way (if one exists) of doing this with Context.stopService().
You can not write
getIntent()
method in a class extending Service. So I think using getExtra() won't work.
My suggetion is that use static member in class that extends Activity for passing information to service & it in service as normal static member access in outside class i.e.
Classname.yourobject
.
see this link for other option
http://developer.android.com/resources/faq/framework.html#3