Service

Service的启动有两种方式:context.startService()context.bindService()。

1.context.startService()

使用context.startService() 启动Service是会会经历:
context.startService()  ->onCreate()- >onStartCommand()->Service running
context.stopService() | ->onDestroy() ->Service stop 

使用方法 : 创建一个类继承 Service,复写里面的上述几个方法即可,然后在Activity中 跳转到相应的service :

开启一个service:

Intent intent = new Intent(MainActivity.this,playService.class);

startService(intent);

结束一个service :

Intent intent = new Intent(MainActivity.this,playService.class);
stopService(intent);


2.context.bindService()

BindService和Started Service都是Service,有什么地方不一样呢:

1. Started Service中使用StartService()方法来进行方法的调用,调用者和服务之间没有联系,即使调用者退出了,服务依然在进行【onCreate()-  >onStartCommand()->startService()->onDestroy()】,注意其中没有onStart(),主要是被onStartCommand()方法给取代了,onStart方法不推荐使用了。

2. BindService中使用bindService()方法来绑定服务,调用者和绑定者绑在一起,调用者一旦退出服务也就终止了【onCreate()->onBind()->onUnbind()->onDestroy()】。

public class MainActivity extends Activity {
    private Button startBtn;
    private Button stopBtn;
    private boolean flag;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        flag = false;
        //设置
        startBtn = (Button)this.findViewById(R.id.startBtn);
        stopBtn = (Button)this.findViewById(R.id.stopBtn);
        startBtn.setOnClickListener(listener);
        stopBtn.setOnClickListener(listener);
    }
    
    private OnClickListener listener = new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.startBtn:
                bindService();
                break;
            case R.id.stopBtn:
                unBind();
                break;
            default:
                break;
            }
        }
        
        
    };
    
    private void bindService(){
        Intent intent = new Intent(MainActivity.this,BindService.class);
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }
    
    private void unBind(){
        if(flag == true){
            unbindService(conn);
            flag = false;
        }
    }
    
    private ServiceConnection conn = new ServiceConnection() {
        
        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
            
        }
        
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            MyBinder binder = (MyBinder)service;
            BindService bindService = binder.getService();
            bindService.MyMethod();
            flag = true;
        }
    };
    
}

public class BindService extends Service {

    private static final String TAG = "BindService";

    public void MyMethod(){
        Log.i(TAG, "BindService-->MyMethod()");
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return myBinder;   //一些后台操作比如播放MP3写在这里
    }
    
    public class MyBinder extends Binder{
        
        public BindService getService(){
            return BindService.this;
        }
    }
    
    private MyBinder myBinder = new MyBinder();
}

方法过程如下:

Intent intent = new Intent(MainActivity.this,BindService.class)->新建了BindService对象->新建了MyBinder对象

->bindService(intent, conn, Context.BIND_AUTO_CREATE);->onBind()函数  -----传递MyBinder对象------->onServiceConnected()

 --> 通过传递的Binder对象获取刚刚和Binder对象对应的BindService 对象  -->调用Service中定义的方法。

    这个其中必须通过Binder对象,因为是通过Binder对象来传递的,通过Binder对象获取Service对象,然后获取所需的服务,所以Service必须实现Binder,以便传递和使用。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值