Android学习-Service运用、绑定及生命周期

本文深入解析了Android中Service的工作原理,包括如何启动、停止服务,以及服务的生命周期。通过实例代码,展示了Service如何在后台持续运行,实现与服务器的数据交互等功能。

Serive即app中的后台服务功能,可在后台继续运行,用以与服务器进行接收、发送数据等功能
MainAty:

private Intent intent;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intent=new Intent(MainActivity.this,MyService.class);
        findViewById(R.id.btnStartService).setOnClickListener(this);
        //代码优化,将同样的指令放一起;
        findViewById(R.id.btnStopService).setOnClickListener(this);
        findViewById(R.id.btnBindService).setOnClickListener(this);
        findViewById(R.id.btnUnbindService).setOnClickListener(this);
            }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnStartService:
                startService(intent);
                break;
            case R.id.btnStopService:
                stopService(intent);
                break;
            case R.id.btnBindService://绑定服务
                bindService(intent,this,Context.BIND_AUTO_CREATE);
                break;
            case R.id.btnUnbindService:
                unbindService(this);
                break;
    }
}
    @Override
    //绑定服务
    public void onServiceConnected(ComponentName name, IBinder service) {
        System.out.println("Service connected");
    }
    @Override
    public void onServiceDisconnected(ComponentName name) {
        System.out.println("Service disconnected");
    }
}

新建Service:

public class MyService extends Service {
public MyService() {
    }
    @Override
    public IBinder onBind(Intent intent) {
        return  new Binder();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //在启动服务后运行
        new Thread(){
            public void run(){
                super.run();
                 while(true){
                     System.out.println("正在运行");
                     try{
                         sleep(1000);
                     }catch (InterruptedException e){
                         e.printStackTrace();
                     }
                 }
            }
        }.start();
        return super.onStartCommand(intent, flags, startId);
    }
}

Service生命周期
主要有onCreate和onDestory两个函数
依然使用上面的例子,再MyService中新增开关,用以控制服务的开启与关闭

public class MyService extends Service {
    private  boolean serviceRunning=false;//默认值为false
    public MyService() {
    }
    @Override
    public IBinder onBind(Intent intent) {
        return  new Binder();
    }
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }
     @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("service create");
        serviceRunning=true;
        //点击启动服务,变量为true,则显示正在运行
        new Thread(){
            public void run(){
                super.run();
                 while(serviceRunning){
                 //此处判断为True则继续,若false则跳出
                     System.out.println("正在运行");
                     try{
                         sleep(1000);
                     }catch (InterruptedException e){
                         e.printStackTrace();
                     }
                 }
            }
        }.start();
    }
    @Override
    public void onDestroy() {
    //点击停止服务,运行此函数,更改变量为false,跳出循环
        super.onDestroy();
        System.out.println("service destory");
        serviceRunning=false;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值