设为首页 加入收藏

TOP

绑定服务(二)
2017-10-13 10:37:08 】 浏览:4611
Tags:绑定 服务
android.content.ServiceConnection; 11 import android.os.Bundle; 12 import android.os.IBinder; 13 import android.util.Log; 14 import android.view.View; 15 16 public class Activity01 extends Activity{ 17 private ServiceConnection conn; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 // TODO Auto-generated method stub 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity01); 23 conn=new ServiceConnection(){ 24 /** 25 * 当服务访问者与服务绑定成功后调用 26 */ 27 @Override 28 public void onServiceConnected(ComponentName arg0, IBinder service) { 29 Log.d("fanfan", "onServiceConnected"); 30 MyServiceBinder binder=(MyServiceBinder)service; 31 //实现了访问者调用服务者的方法 32 binder.show(); 33 } 34 /** 35 * 当service崩溃或被系统强制杀死后调用 36 */ 37 @Override 38 public void onServiceDisconnected(ComponentName arg0) { 39 Log.d("fanfan", "onServiceDisconnected"); 40 } 41 42 }; 43 } 44 45 public void onClick(View view){ 46 Intent intent=new Intent(); 47 intent.setClass(this, myService.class); 48 switch(view.getId()){ 49 case R.id.btn_bind://绑定服务 50 bindService(intent, conn, Context.BIND_AUTO_CREATE); 51 break; 52 case R.id.btn_unbind://解绑服务 53 unbindService(conn); 54 break; 55 } 56 } 57 }

 

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android彻底组件化方案实践 下一篇Android彻底组件化demo发布

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目