startService与bindService开启服务的生命周期的区别

本文详细解析了Android中通过startService和bindService两种方式开启服务的生命周期。startService方式下,服务独立运行,状态不受开启者影响;bindService方式下,服务与开启者绑定,状态相关。文章提供了MainActivity和MyService的代码示例,展示了不同方式下服务的启动、运行和销毁过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、startService方式开启服务的生命周期
服务会执行onCreate() 和onStartCommand()方法,服务处于运行状态, 直到自身调用stopSelf()方法或者其他组件调用stopService()方法时服务停止, 最终被系统销毁。
服务会长期的在后台运行,并且服务的状态与开启者的状态没有关系。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
//开启服务
    public void st(View view) {
        Intent intent=new Intent(this,MyService.class);
        startService(intent);
    }
//销毁服务
    public void clo(View view) {
        Intent intent=new Intent(this,MyService.class);
        stopService(intent);
    }
}

public class MyService extends Service {
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("zg","onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("zg","onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("zg","onDestroy");
    }
}

• 2、bindService方式开启服务的生命周期
服务会执行onCreate() 和 onBind()方法,服务处于绑定状态, 客户端 通过unbindService()方法关闭连接,解除绑定时,系统将直接销毁服务。
服务与开启者的状态有关,当调用者销毁了,服务也会被销毁。 。

   MainActivity页面
public class MainActivity extends AppCompatActivity {
    private MyService.Mybinder mybinder;
    private MyConn myConn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
//绑定服务
    public void bind(View view) {
        if(myConn==null){
         myConn=new MyConn();
        }
        Intent intent=new Intent(this,MyService.class);
        bindService(intent,myConn,BIND_AUTO_CREATE);

    }
//调用服务中的方法
    public void call(View view) {
        if (myConn!=null){
        mybinder.callMethodInServicr();}
    }
//解绑服务
    public void unbind(View view) {
        if (myConn!=null){
            unbindService(myConn);
            myConn=null;
        }
    }

//创建Myconn类,用于实现连接服务
    private class MyConn implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mybinder=(MyService.Mybinder) service;
            Log.i("Service","绑定成功");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}


public class MyService extends Service {
    public MyService() {
    }
    //安全暴露服务中的某个方法
    class Mybinder extends Binder{
        public  void  callMethodInServicr(){
            methodInService();
        }
    }
//返回方法
    @Override
    public IBinder onBind(Intent intent) {
        Log.i("Server","onBind");
        Mybinder mybinder=new Mybinder();
        return mybinder;
    }
    //自定义方法
    public void methodInService(){
        Log.i("Server","自定义方法被调用");
    }
//创建服务
    @Override
    public void onCreate() {
        Log.i("Server","onCreate");
        super.onCreate();
    }
//销毁服务
    @Override
    public void onDestroy() {
        Log.i("Server","onDestroy");
        super.onDestroy();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值