• 03_Service的绑定


    Start Service启动的Service,Application退出,Service也不会退出;

    Bind Service启动的Service,Application退出,Service就会退出。

    Start Service和Stop Service仅仅是启动服务,并不能进行通信。通过Bind Service与服务进行绑定,才能与服务通信。

    package com.example.servdemo;
    
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Context;
    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;
    
    public class MainActivity extends Activity implements OnClickListener, ServiceConnection {
    
        private Button btnStartService, btnStopService, btnBindService, btnUnbindService, btnGetCurrentNum;
        private Intent serviceIntent;
        private EchoService echoService = null;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            serviceIntent = new Intent(this, EchoService.class);
            
            btnStartService = (Button) findViewById(R.id.btnStartService);
            btnStopService = (Button) findViewById(R.id.btnStopService);
            btnBindService = (Button) findViewById(R.id.btnBindService);
            btnUnbindService = (Button) findViewById(R.id.btnUnbindService);
            btnGetCurrentNum = (Button) findViewById(R.id.btnGetCurrentNum);
            
            btnStartService.setOnClickListener(this);
            btnStopService.setOnClickListener(this);
            btnBindService.setOnClickListener(this);
            btnUnbindService.setOnClickListener(this);
            btnGetCurrentNum.setOnClickListener(this);
        }
    
    
        @Override
        public void onClick(View v) {
            
            switch (v.getId()) {
            
            case R.id.btnStartService:
                startService(serviceIntent);
                break;
    
            case R.id.btnStopService:
                stopService(serviceIntent);
                break;
                
            case R.id.btnBindService:
                bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);
                break;
                
            case R.id.btnUnbindService:
                unbindService(this);
                echoService = null;
                break;
                
            case R.id.btnGetCurrentNum:
                if (echoService != null) {
                    System.out.println("The Number is  "+echoService.getCurrentNum());
                }
                break;
            }
        }
    
        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            System.out.println("onServiceConnected!");
            
            echoService = ( (EchoService.EchoServiceBinder)binder ).getService();
        }
    
        @Override
        public void onServiceDisconnected(ComponentName name) {
            
        }
    
    }
    package com.example.servdemo;
    
    import java.util.Timer;
    import java.util.TimerTask;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    
    public class EchoService extends Service {
    
        @Override
        public IBinder onBind(Intent intent) {
            System.out.println("onBind!");
            return echoServiceBinder;
        }
        
        private final EchoServiceBinder echoServiceBinder = new EchoServiceBinder();
        
        public class EchoServiceBinder extends Binder{
            public EchoService getService() {
                return EchoService.this;
            }
        }
        
        public int getCurrentNum(){
            return i;
        }
        
        @Override
        public void onCreate() {
            // TODO Auto-generated method stub
            System.out.println("Service Start!");
            startTimer();
            super.onCreate();
        }
        
        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            System.out.println("Service Stop!");
            stopTimer();
            super.onDestroy();
        }
        
        private int i = 0;
        
        public void startTimer(){
            if(timer == null){
                timer = new Timer();
                timerTask = new TimerTask() {
                    
                    @Override
                    public void run() {
                        i++;
                        System.out.println(i);
                    }
                };
                
                timer.schedule(timerTask, 1000, 1000);
            }
        }
        
        public void stopTimer(){
            if(timer != null){
                timerTask.cancel();
                timer.cancel();
                
                timer = null;
                timerTask = null;
            }
        }
        
        private Timer timer = null;
        private TimerTask timerTask = null;
    
    }
  • 相关阅读:
    jQuery基础---filter()和find()
    js数组去重的4个方法
    各种排序算法的分析及java实现
    js三种对话框
    js数组与字符串的相互转换
    java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result异常的解决方法
    Django REST framework视图
    Django REST framework序列化
    Django REST framework简介
    Django REST framework认证、权限、频率
  • 原文地址:https://www.cnblogs.com/510602159-Yano/p/4039157.html
Copyright © 2020-2023  润新知