Service的onServiceConnected没有被调用

转:http://blog.csdn.net/shaojie519/article/details/6641066

Service是一种运行在后台的服务,一般很少与用户交互,所以没有可视化界面。

我们可以通过startService() 或者使用bindService()方法来绑定一个存在的service。

bindService是异步调用和Service进行绑定, 如果绑定成功, 则会调用ServiceConnection的onServiceConnected

当调用bindService方法后就会回调Activity的onServiceConnected,在这个方法中会向Activity中传递一个IBinder的实例,Acitity需要保存这个实例

在Service中需要创建一个实现IBinder的内部类(这个内部类不一定在Service中实现,但必须在Service中创建它)。

在OnBind()方法中需返回一个IBinder实例,不然onServiceConnected方法不会调用。

[java]  view plain copy
  1.   
[java]  view plain copy
  1. import android.app.Activity;  
  2. import android.content.ComponentName;  
  3. import android.content.Intent;  
  4. import android.content.ServiceConnection;  
  5. import android.os.Bundle;  
  6. import android.os.IBinder;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.Toast;  
  11.   
  12. public class ServiceActivity extends Activity {  
  13.     /** Called when the activity is first created. */  
  14.     private Button start;  
  15.     private Button stop;  
  16.     private Button bind;  
  17.     private Button unbind;  
  18.       
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.         start  =(Button) findViewById(R.id.start);  
  24.         stop = (Button) findViewById(R.id.stop);  
  25.         bind  = (Button) findViewById(R.id.bind);  
  26.         unbind  = (Button) findViewById(R.id.unbind);  
  27.         //启动一个service   
  28.         start.setOnClickListener(new OnClickListener() {  
  29.               
  30.             @Override  
  31.             public void onClick(View arg0) {  
  32.                 // TODO Auto-generated method stub  
  33.             Intent intent = new Intent();  
  34.             System.out.println("start....");  
  35.             intent.setAction("com.shao.service.action.MyService");  
  36.             startService(intent);  
  37.             }  
  38.         });  
  39.         //结束一个service    
  40.         stop.setOnClickListener(new OnClickListener() {  
  41.               
  42.             @Override  
  43.             public void onClick(View arg0) {  
  44.                 // TODO Auto-generated method stub  
  45.             Intent intent = new Intent();  
  46.             System.out.println("stop....");  
  47.             intent.setAction("com.shao.service.action.MyService");  
  48.             stopService(intent);  
  49.             }  
  50.         });  
  51.           
  52.       //绑定一个service  
  53.      bind.setOnClickListener(new OnClickListener() {  
  54.               
  55.             @Override  
  56.             public void onClick(View arg0) {  
  57.                 // TODO Auto-generated method stub  
  58.             Intent intent = new Intent();  
  59.             System.out.println("bind..");  
  60.             intent.setAction("com.shao.service.action.MyService");  
  61.             bindService(intent, conn, BIND_AUTO_CREATE);  
  62.             }  
  63.         });  
  64.      //解除绑定的service  
  65.      unbind.setOnClickListener(new OnClickListener() {  
  66.               
  67.             @Override  
  68.             public void onClick(View arg0) {  
  69.                 // TODO Auto-generated method stub  
  70.             Intent intent = new Intent();  
  71.             System.out.println("unbind..");  
  72.             intent.setAction("com.shao.service.action.MyService");  
  73.             unbindService(conn);  
  74.             }  
  75.         });  
  76.     }  
  77.       
  78.     private ServiceConnection conn = new ServiceConnection() {  
  79.           
  80.         @Override  
  81.         public void onServiceDisconnected(ComponentName arg0) {  
  82.             // TODO Auto-generated method stub  
  83.             Toast.makeText(ServiceActivity.this,"disconn successful" ,Toast.LENGTH_LONG);  
  84.         }  
  85.           
  86.         @Override  
  87.         public void onServiceConnected(ComponentName name, IBinder service) {  
  88.             // TODO Auto-generated method stub  
  89.             Toast.makeText(ServiceActivity.this,"conn successful" ,Toast.LENGTH_LONG);  
  90.         }  
  91.     };  
  92. }  


[java]  view plain copy
  1.   

MyService文件:

[java]  view plain copy
  1.   
[java]  view plain copy
  1. import android.app.Service;  
  2. import android.content.Intent;  
  3. import android.os.Binder;  
  4. import android.os.IBinder;  
  5. import android.os.Parcel;  
  6. import android.widget.Toast;  
  7.   
  8. public class MyService extends Service {  
  9.     @Override  
  10.     public IBinder onBind(Intent intent) {  
  11.         // TODO Auto-generated method stub  
  12.         System.out.println("onBind.....");  
  13.          IBinder result = null;    
  14.             if ( null == result ) result = new MyBinder() ;  
  15.         Toast.makeText(this"onBind",Toast.LENGTH_LONG);  
  16.         return result;  
  17.     }  
  18.   
  19.     @Override  
  20.     public void onCreate() {  
  21.         // TODO Auto-generated method stub  
  22.         super.onCreate();  
  23.         System.out.println("onCreate.....");  
  24.         Toast.makeText(this"onCreate",Toast.LENGTH_LONG);  
  25.     }  
  26.   
  27.     @Override  
  28.     public void onDestroy() {  
  29.         // TODO Auto-generated method stub  
  30.         super.onDestroy();  
  31.         System.out.println("onDestory.....");  
  32.         Toast.makeText(this"onDestroy",Toast.LENGTH_LONG);  
  33.     }  
  34.   
  35.     @Override  
  36.     public void onStart(Intent intent, int startId) {  
  37.         // TODO Auto-generated method stub  
  38.         super.onStart(intent, startId);  
  39.         System.out.println("onStart...");  
  40.         Toast.makeText(this"onStart",Toast.LENGTH_LONG);  
  41.     }  
  42.     public class MyBinder extends Binder {    
  43.         //此方法是为了可以在Acitity中获得服务的实例    
  44.             public MyService getService() {    
  45.                 return MyService.this;    
  46.             }    
  47.            
  48.         }  
  49. }  


Service需在mainifest文件中声明:

   <service android:name="MyService">

                 <intent-filter>
                 <action android:name="com.shao.service.action.MyService"/>
              </intent-filter>
             </service>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值