Service的onServiceConnected没有被调用

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

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

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

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

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

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


import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ServiceActivity extends Activity {
    /** Called when the activity is first created. */
	private Button start;
	private Button stop;
	private Button bind;
	private Button unbind;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        start  =(Button) findViewById(R.id.start);
        stop = (Button) findViewById(R.id.stop);
        bind  = (Button) findViewById(R.id.bind);
        unbind  = (Button) findViewById(R.id.unbind);
        //启动一个service 
        start.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			Intent intent = new Intent();
			System.out.println("start....");
			intent.setAction("com.shao.service.action.MyService");
			startService(intent);
			}
		});
        //结束一个service  
        stop.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			Intent intent = new Intent();
			System.out.println("stop....");
			intent.setAction("com.shao.service.action.MyService");
			stopService(intent);
			}
		});
        
      //绑定一个service
     bind.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			Intent intent = new Intent();
			System.out.println("bind..");
			intent.setAction("com.shao.service.action.MyService");
			bindService(intent, conn, BIND_AUTO_CREATE);
			}
		});
     //解除绑定的service
     unbind.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			Intent intent = new Intent();
			System.out.println("unbind..");
			intent.setAction("com.shao.service.action.MyService");
			unbindService(conn);
			}
		});
    }
    
    private ServiceConnection conn = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			// TODO Auto-generated method stub
			Toast.makeText(ServiceActivity.this,"disconn successful" ,Toast.LENGTH_LONG);
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			Toast.makeText(ServiceActivity.this,"conn successful" ,Toast.LENGTH_LONG);
		}
	};
}


MyService文件:

 import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.os.Parcel; import android.widget.Toast; public class MyService extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub System.out.println("onBind....."); IBinder result = null; if ( null == result ) result = new MyBinder() ; Toast.makeText(this, "onBind",Toast.LENGTH_LONG); return result; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); System.out.println("onCreate....."); Toast.makeText(this, "onCreate",Toast.LENGTH_LONG); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); System.out.println("onDestory....."); Toast.makeText(this, "onDestroy",Toast.LENGTH_LONG); } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); System.out.println("onStart..."); Toast.makeText(this, "onStart",Toast.LENGTH_LONG); } public class MyBinder extends Binder { //此方法是为了可以在Acitity中获得服务的实例 public MyService getService() { return MyService.this; } } } 
 

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、付费专栏及课程。

余额充值