I'm trying to start a service after the device has been started. The problem is that the service needs some arguments which it normally gets this way:
public class ServiceClass extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
searchString = (String) intent.getExtras().get(GET_SEARCHSTRING_AFTER_START);
[...]
return START_STICKY;
public static final String GET_SEARCHSTRING_AFTER_START = "SEARCHVALUE";
public class OnStartupStarter[...]
}
But when the service should be started via a BroadcastReceiver when the device has started I can't put the searchString because this is given by an activity. When the service starts after the device has been started the service should start with the searchString it had before the device was turned off.
The BroadcastReceiver is a subclass from the service's class:
public static class OnStartupStarter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
/* TODO: Start the service with the searchString it
* had before the device has been turned off...
*/
}
}
Normally the service is started like this:
private OnCheckedChangeListener ServiceSwitchCheckedStatusChanged
= new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Intent s = new Intent(getBaseContext(), ServiceClass.class);
s.putExtra(ServiceClass.GET_SEARCHSTRING_AFTER_START, <ARGUMENT>);
if(isChecked)
startService(s);
else
stopService(s);
}
};
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…