Android service tip!

来源:互联网 发布:淘宝账号被冻结支付宝 编辑:程序博客网 时间:2024/05/18 12:32

1. Service is running main application thread, same as the UI thread, startService or bindService will not create a new thread, so if there are some long time work in service, service should create a new thread for them.


2. The bindService() is a asynchronous, the return value doesn't mean that the service is connected successfully, so client should judge if the connection to service is successfully when onServiceConnected is called.


3. Context related:

If the client is an activity, there are two important steps to take to ensure that the binding survives a configuration change, like a screen rotation:

1) Instead of calling bindService() on the activity itself, call bindService() on the Application Context (obtained via getApplicationContext()).

2. Make sure the ServiceConnection gets from the old instance of the activity to the new one, probably via onRetainNonConfigurationInstance().

This allows the binding to persist between activity instances.


4.

原创粉丝点击