BindService
来源:互联网 发布:400软件 编辑:程序博客网 时间:2024/04/27 23:19
一样是两个button按钮:
<Button android:id="@+id/bt_kaishi" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TableOne" /><Button android:id="@+id/bt_jieshu" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TableTwo" />
写一个Bind类继承Service:
public class Bind extends Service{private String tag="bindservice";@Overridepublic void onCreate() {Log.e(tag, "onCreate");super.onCreate();} @Overridepublic IBinder onBind(Intent intent) {Log.e(tag, "onBind");return null;}@Overridepublic boolean onUnbind(Intent intent) {Log.e(tag, "onUnbind");return super.onUnbind(intent);}@Overridepublic void onDestroy() {Log.e(tag, "onDestroy");super.onDestroy();}public void getLast(){Log.e("", "下一曲");}public class MyBind extends Binder{public Bind getservice(){return Bind.this;}}}
Mainactivity:
public class MainActivity extends Activity implements OnClickListener { private Button bt_kaishi,bt_jieshu; private Conn conn;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); conn = new Conn(); initView(); }private void initView() {bt_kaishi = (Button) findViewById(R.id.bt_kaishi);bt_jieshu = (Button) findViewById(R.id.bt_jieshu);bt_kaishi.setOnClickListener(this);bt_jieshu.setOnClickListener(this);}@Overridepublic void onClick(View v) {if (R.id.bt_kaishi==v.getId()) {Intent intent = new Intent(MainActivity.this,Bind.class);bindService(intent, conn, Context.BIND_AUTO_CREATE);}else {unbindService(conn);}}//绑定服务监听private class Conn implements ServiceConnection{//绑定服务成功的监听 service:服务绑定成功后的返回值 onbund:方法的返回值@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {MyBind bind=(MyBind) service;bind.getservice().getLast();}//因为异常,绑定服务失败的监听@Overridepublic void onServiceDisconnected(ComponentName name) {}}}
0 0
- bindService
- BindService
- BindService
- BindService
- BindService
- BindService
- bindService
- BindService
- BindService()
- 理解BindService
- BindService总结
- AIDL bindService
- bindservice用法
- 10 BindService
- bindService用法
- startService bindService
- BindService详解
- bindservice 理解
- css3垂直居中的几种方法
- web学习
- CodeForces 282CXOR and OR
- Python 元组、集合和字典 实例
- 优雅编程之这样考虑字符性能,你就“正常”了(二十二)
- BindService
- AIDL随记(绑定本地和远程service)
- curl模拟post请求提交
- 螺旋数组的C语言实现
- 飞机游戏中子弹与飞机的移动与边界源码
- 在phpstorm中利用post方法提交form表单后,在响应页面获取不到数据的解决办法
- Struts2 数据校验
- 链表与迭代器
- 【给将来学神的算法详解--高精】(1)(我的)高精度模板