How can I pass an Intent to a Service when starting it (but only sometimes)
So, I'm working on an Android application for a school project, and have hit a strange bug towards the end of the development. Part of the application is a Service that runs in the background and checks in with a server every so often, but there needs to be an option whether to run the service in the background or to rather use manual check-ins. In order to avoid duplicating code, what I've tried to do is pass an Intent along to the service when it starts with a boolean value along the lines of "Force an update once, then stop." However, My Service doesn't seem to be getting this value.
The code that starts the service:
Intent intent = new Intent(this, EmpCheckinService.class);
intent.putExtra("singleCheckInOnly", true);
intent.putExtra("locationString", location);
startService(intent);
And the code in the Service class:
// This is the old onStart method that will be called on the pre-2.0
// platform. On 2.0 or later we override onStartCommand() so this
// method will not be called.
@Override
public void onStart(Intent intent, int startId) {
examineIntent(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
examineIntent(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
private void examineIntent(Intent intent) {
try {
singleCheckInOnly = intent.getExtras().getBoolean("singleCheckInOnly", false);
locationString = intent.getExtras().getString("locationString");
} catch (Exception e) {
// Don't need to 开发者_JAVA百科do anything here, just prevent from crashing if the keys aren't found
}
}
As you can see, I've got onStart and onStartCommand present to allow it to work on 1.5 or 2.1+ devices, but it never hits either of those functions. Can anyone point me in the right direction here?
EDIT: I think I found the issue. Is onCreate called before onStartCommand?
Is onCreate called before onStartCommand?
Yes.
This would be significantly simpler and more user-friendly if you:
Step #1: Create an IntentService
rather than a Service
, and
Step #2: Use AlarmManager
to send Intents
to the service for the periodic checks with the server
Then, from the service's standpoint, there is no difference between the timer-based check and the user-initiated check.
if you have more than 1 extra to put, then it is preferred that you use bundle
Tos send extras use-
Intent i = new Intent(sender.this,receiver.class);
Bundle extras = new Bundle();
extras.putString("key1",element);
extras.putString("key1",element);
extras.putString("key2",element2);
extras.putString("key3",element3);
extras.putString("key4",element4);
......so on
i.putExtras(extras);
to receive use-
Bundle extras2 = getIntent().getExtras();
if (extras != null){
final String str = extras2.getString("key1");
final String str = extras2.getString("key2");
final String str = extras2.getString("key3");
final String str = extras2.getString("key4");
final String str = extras2.getString("key5");
......so on
精彩评论