zoukankan      html  css  js  c++  java
  • Service的使用

    一、Service的适用范围

      1.本地服务(Local Service): 应用程序的内部(单个APP)

        startServcie  stopService  stopSelf  stopSelfResult

        bindService  unbindService

      2.远程服务 (Remote  Service) :  Android系统内部应用程序之间(多个APP)

        定义IBinder的接口

      3.Service的生命周期

        

      4.startService和bindService的各自特点

        

        
       //通过ServiceConnect对象的相关方法可以得到Service的对象
        ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
        service = ((MyBinderService.MyBind)binder).getService();
        }


        //通过Ibinder接口实例返回给ServiceConnect对象给启动源
        public class MyBinderService extends Service {
        private String TAG = "info";

        public class MyBind extends Binder{
        public MyBinderService getService(){
        return MyBinderService.this;
        }
        }
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
        Log.i(TAG, "MyBinderService-onBind: ");
        return new MyBind();
        }
        intent = new Intent(MainActivity.this , MyBinderService.class);
        bindService(intent, conn(ServiceConnect), Service.BIND_AUTO_CREATE);

            

  • 相关阅读:
    JavaScript——封装
    Vue.js——component(组件)
    MySQL数据库——安装教程(5.7版本)
    Vue.js——循环(Java、JSTL标签库、数据库)
    Vue.js——理解与创建使用
    JavaScript——闭包
    自定义最小索引优先队列
    自定义最大索引优先队列
    自定义最小优先队列
    自定义最大优先队列
  • 原文地址:https://www.cnblogs.com/liunx1109/p/9757891.html
Copyright © 2011-2022 走看看