如何获得参数为设备已启动之后启动的服务? [英] How to get arguments for starting a service after device has been started?

查看:145
本文介绍了如何获得参数为设备已启动之后启动的服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在设备启动后启动服务。问题是,该服务需要一些参数,它通常可以获得这样:

 公共类ServiceClass延伸服务{
    @覆盖
    公众诠释onStartCommand(意向意图,诠释标志诠释startId){
        搜索字符串=(字符串)intent.getExtras()获得(GET_SEARCHSTRING_AFTER_START)。
        [...]
        返回START_STICKY;    公共静态最后弦乐GET_SEARCHSTRING_AFTER_START =SEARCHVALUE;    公共类OnStartupStarter [...]}

但是,当服务应该通过当装置已开始我不能把searchString的,因为这是由活动给出一个BroadcastReceiver启动。当该装置启动后的服务启动服务应与searchString的它有设备被关闭之前开始。


的广播接收器是从服务的类的子类:

 公共静态类OnStartupStarter扩展广播接收器{
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){        / * TODO:开始的搜索字符串的服务,它
         *有该设备已被关闭之前...
         * /
    }
}


通常情况下,服务启动这样的:

 私人OnCheckedChangeListener ServiceSwitchCheckedStatusChanged
=新OnCheckedChangeListener(){
     公共无效onCheckedChanged(CompoundButton buttonView,
            布尔器isChecked){
        意图S =新意图(getBaseContext(),ServiceClass.class);        s.putExtra(ServiceClass.GET_SEARCHSTRING_AFTER_START,<参数>);
        如果(器isChecked)
            startService(多个);
        其他
            stopService(多个);
    }
};


解决方案

在活动共享preference 保存最后一个搜索字符串的onPause 方法,检索过去的搜索字符串,当您收到 BootCompleted 广播和通常开始为您服务。

活动:

 保护无效的onPause(){
    super.onPause();    共享preferences preF = getShared preferences(MY_ preF,MODE_PRIVATE);
    共享preferences.Editor编辑器= pref.edit();
    editor.putString(last_search,mLastSearch);
    editor.commit();
}

广播接收器:

 公共静态类OnStartupStarter扩展广播接收器{
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        字符串行动= intent.getAction();        如果(action.equals(Intent.ACTION_BOOT_COMPLETED)){
            共享preferences preF = context.getShared preferences(MY_ preF,MODE_PRIVATE);
            字符串lastSearch = pref.getString(last_search,NULL);
            如果(lastSearch!= NULL){
                // TODO:启动服务
            }
        }
    }
}

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);
    }
};

解决方案

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
            }
        }
    }
}

这篇关于如何获得参数为设备已启动之后启动的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆