Android中如何绑定Service并从服务获取数据?

How to bind Service and get data from the service in Android?

我想绑定到一个 Service.I 想在 mainActivity.Where 的 Resume 函数之前从服务获取数据 我应该在 main activity 中调用 bindService() 函数吗?

求助! 这是代码

public class MainActivity extends AppCompatActivity{


    ArrayList<City> selected_cities;
    ArrayList<City> cities;
    RecyclerView Rview;
    CitySelectedListAdapter adapter;
    final int REQUEST_CODE = 1;
    ICityDao dao;

    TimezoneService timezoneService ;
    boolean bound = false;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            timezoneService = ((TimezoneService.LocalBinder ) service).getService();
            Log.d("Service connected","Service Connected ");
            bound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        bound = false;
        }
    } ;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dao = new CItyDao(this);
        selected_cities = new ArrayList<City>();
        showMessage("Created");

        create_City_list();


        Intent intent  = new Intent(this,TimezoneService.class);
        bindService(intent,connection,Context.BIND_AUTO_CREATE);

        Log.d("created","activity created ");


    }
    public void onStart() {
        super.onStart();
    }
    public void onResume()
    {
        super.onResume();
        showMessage("Resumed");
        Log.d("Resume","resume ");
        selected_cities = cities.get(0).load(dao);
        if(selected_cities.size()!=0) {
            CreateListView();
        }

        for (int i =0;i< selected_cities.size();i++)
        {
            load_checkbox(selected_cities.get(i).getName());

        }


    }
}


这里是我想调用服务中编写的connectTimezonedb() 的服务代码,并在主activity 恢复之前从服务的这个函数中获取数据。请帮忙!

public class TimezoneService extends Service {

    private final IBinder binder = new LocalBinder();
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return START_NOT_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d("Onbind","onBind ");
        ConenctTimezoneDb();
        return binder;
    }

    public class LocalBinder extends Binder
    {
        public TimezoneService getService()
        {
            return TimezoneService.this;
        }
    };
    public void ConenctTimezoneDb()
    {
        Log.d("connect timezone","connected with time zone db");

        try{
            URL url = new URL("http://api.timezonedb.com/v2.1/list-time-zone?&format=xml&country=US");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

    }

}

尽快绑定,所以 onCreate 是合适的位置,但是

I want to get data from Service before on Resume function of mainActivity.

您根本无法确保这一点。 ServiceActivity 有自己独立的生命周期,你不能确定 Service 甚至在 Activity 中的 onResume 被调用之前就开始了

顺便说一句。您在 url...

中透露了 API 的密钥