以绑定的方式来启动service
2014-09-19 09:23
218 查看
先说下原理,之前我们的启动service就是用startService来启动的,这是显式启动。启动后我们无法得到service中的数据,也无法知道它执行的状态,如果我们要启动它的activity和它建立一个联系,获得他的数据或者是执行其内部的方法时就需要隐式启动了。
关键原理在于使用一个binder来传递数据,并且要给service配置一个action作为标签。
BindService
这里binder就可以通过其内部的getcount方法来向外部传递数据了。接下来要配置service了,这里的action就是用来给启动时作为标记的。
最后是在activity中通过button来启动service
关键原理在于使用一个binder来传递数据,并且要给service配置一个action作为标签。
BindService
package com.example.service; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; public class BindService extends Service{ String tag = getClass().getSimpleName(); private int count = 123; private MyBinder binder = new MyBinder(); //建立一个binder对象,用来在onbind中返回 public class MyBinder extends Binder{ public int getCount() { return count; } } /* (非 Javadoc) * @see android.app.Service#onBind(android.content.Intent) * 连接时执行的方法 */ @Override public IBinder onBind(Intent intent) { // TODO 自动生成的方法存根 Log.i(tag, "onBind"); return binder; } /* (非 Javadoc) * @see android.app.Service#onUnbind(android.content.Intent) * 断开连接时回调方法 */ @Override public boolean onUnbind(Intent intent) { // TODO 自动生成的方法存根 Log.i(tag, "onUnbind"); return super.onUnbind(intent); } @Override public void onCreate() { // TODO 自动生成的方法存根 super.onCreate(); Log.i(tag, "onCreat"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO 自动生成的方法存根 Log.i(tag, "onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO 自动生成的方法存根 super.onDestroy(); Log.i(tag, "onDestroy"); } }
这里binder就可以通过其内部的getcount方法来向外部传递数据了。接下来要配置service了,这里的action就是用来给启动时作为标记的。
<service android:name="com.example.service.BindService" > <intent-filter> <action android:name="com.example.service.BindService" /> </intent-filter> </service>
最后是在activity中通过button来启动service
package com.example.service; import android.app.Activity; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.View; public class MainActivity extends Activity { private BindService.MyBinder binder; private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { //连接成功时,获取service的binder对象 binder = (BindService.MyBinder)service; } @Override public void onServiceDisconnected(ComponentName name) { // TODO 断开连接时 } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void buttonListener(View v) { //设置action,这样启动的时候就知道是启动哪个service了 Intent intent = new Intent("com.example.service.BindService"); switch (v.getId()) { case R.id.binderService_button: //通过这个过滤器来判断绑定哪个service,所以在配置的时候需要配置action bindService(intent, connection, Service.BIND_AUTO_CREATE); break; case R.id.unbinderService_button: unbindService(connection); break; case R.id.getData_button: System.out.println("service 中的数据 = :"+ binder.getCount()); break; default: break; } } }
相关文章推荐
- Android:Service(二)——以绑定方式启动Service
- android 音乐播放 启动方式 (1)利用内部类绑定service传送数据,只适用于单activity
- 旺仔练习:service的普通方式和绑定方式的启动和停止(解绑出现异常,尚未解决)
- 通过绑定的方式启动service
- 旺仔练习:service的普通方式和绑定方式的启动和停止
- android Service的启动方式和重复绑定
- Service 绑定方式启动,生命周期。绑定方式读取服务器数据
- 安卓Service绑定方式启动服务并调用服务中的方法
- Android之Service学习篇一:Service启动方式之startService
- IIS Admin Service 无法启动 ,错误代码-2145318902解决方式
- Service的两种启动方式
- 绑定Service的三种实现方式之使用Messenger
- Android Service 两种启动方式的区别
- android89--Activity怎么和service绑定,怎么在activity中启动自己对应的service
- Service:启动模式和绑定模式
- 用bindService()启动Service的方式与生命周期
- service 两种启动方式的区别
- Linux下将普通软件启动方式做成service方式
- Android之Service学习篇二:Service启动方式之boundService
- 启动service的两种方式:startService与bindService的区别