Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

android - How to get arguments for starting a service after device has been started?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Save last search string in SharedPreference in activity onPause method, retrieve last search string when you receive BootCompleted broadcast and start your service as usually.

Activity:

protected void onPause() {
    super.onPause();

    SharedPreferences pref = getSharedPreferences("my_pref", MODE_PRIVATE);
    SharedPreferences.Editor editor = pref.edit();
    editor.putString("last_search", mLastSearch);
    editor.commit();
}

BroadcastReceiver:

public static class OnStartupStarter extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
            SharedPreferences pref = context.getSharedPreferences("my_pref", MODE_PRIVATE);
            String lastSearch = pref.getString("last_search", null);
            if (lastSearch != null) {
                // TODO: Start service
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...