• Android Service组件理解


    1.什么是Service?
          Service就是一个执行后台任务的模块或组件,但需要注意,它本身默认是运行在主线程(进程)中的,并没有创建新的线程,因此不能在其中做很耗时的任务,类似于Thread和AsyncTask

    2.为什么要使用Service?
           当一个应用程序含有Service,则该程序便会因此有希望在系统中保留更长的时间(特别在系统资源紧张,系统便会选择性kill某些处于后台的应用程序,而假如应用程序有Service便不那么容易被kill掉)
          (慎重使用Service,只有在某些功能、任务等在应用处于后台情况也必须保证正常运行的活着一些监听类行为的功能才可采用,像一般的耗时计算、异步网络请求等对于应用处不处于后台都无影响)

    3.如何使用Service?
            任何Service的使用都必须先自定义一个继承自Service的子类,然后重写特定的方法
             然后在AndroidMainifest.xml中配置声明该Service,如下:
               
                android:exported="false">表示不导出,即本地Service
                   
                       
                   
               

            (1).以startService()方法启动Service,访问者与Service没有关联、没有通信,访问者
                 即使退出,Service仍然运行,一直运行,不管应用程序处在后台还是压根没打开,只有当该应用被删掉之 后,启动的Service才会停止。
                 自定义Service类如下:
    public class MyService extends Service{
       
        @Override
        public void onCreate() {   Service第一次被创建时调用,只会调用一次,除非再次重建,与
    onDestroy对应
            super.onCreate();
            Log.v("ppp", "onCreate");
        }
       
        @Override
        public int onStartCommand(Intent intent, int flags, int startId){
             该Service以调用方法StartService方式启用时调用,每启用一次则调用一次
            return START_STICKY;
        }
       
        @Override
        public IBinder onBind(Intent intent) {
            其实这是以bindService方式启动时需实现的方法,但此处还是要提供一个空实现并返回null
     
            return null;
        }
              
        @Override
        public void onDestroy (){
            super.onDestroy();
            Log.v("ppp", "onDestroy");
        }
       
    }

         
              (2). 以bindService方式启用,访问者与Service之间有关联、能够通信且访问者退出,Service也就终止                 自定义Service类如下:
    public class MyService extends Service{
        private MyBinder binder = new MyBinder(); 给Service定义一个Binder类型的成员变量,供onBind函数返回给访问者,然后访问者便可以通过这个binder调用改Service里的方法了,实现通信
       
        public class MyBinder extends Binder {
            public void ggg() {
               
            }
        }

        @Override
        public void onCreate() {   Service第一次被创建时调用,只会调用一次,除非再次重建,与
    onDestroy对应
            super.onCreate();
            Log.v("ppp", "onCreate");
        }
               
        @Override
        public IBinder onBind(Intent intent) {
    该Service以调用方法bindService方式启用时调用 与onUnbind对应 
            // TODO Auto-generated method stub
            Log.v("ppp", "onBind");
            return binder;
        }
       
        @Override
         public void onRebind(Intent intent) {  重新被绑定时
           
         }

        @Override
        public boolean onUnbind(Intent intent){  调用unBindService方法解除绑定时
            Log.v("ppp", "onUnbind");
            return true;
        }
       
        @Override
        public void onDestroy (){
            super.onDestroy();
            Log.v("ppp", "onDestroy");
        }
       
    }

                  访问者调用方式如下,一般在Activity中进行:
     public class MainActivity extends Activity{
            需要定义一个ServiceConnection对象供bindService传参及回调
            private ServiceConnection cconConnection = new ServiceConnection() {
                
                @Override
                public void onServiceDisconnected(ComponentName name) {
                    // TODO Auto-generated method stub
                    //只有宿主进程异常终止时该方法才会被执行,直接调用unBind方法则不会
                    Log.v("ppp", "onServiceDisconnected");
                }
                
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    // TODO Auto-generated method stub
                    Log.v("ppp", name.toString());
                    Log.v("ppp", service.toString());
                    MyService.MyBinder binder = (MyService.MyBinder)service; 化onBind返回的binder对象
                    binder.ggg();   使用binder对象提供的方法实现通信
                }
            };
     
           绑定Service
           public void button2Click(View v) {
            Intent intent = new Intent();
            intent.setAction("com.example.service.MyService");
       
            bindService(intent, cconConnection, Service.BIND_AUTO_CREATE);
        }


     }  
     
               关于IntentService,只适合startService方式启动,因为其默认把onBind方法返回了null,无法实现访问者与Service的通信,且其本身就是帮忙封装了一个多线程操作
              

            Remote Service(远程Service,即应用程序间的通信) AIDL
            a.编写通信接口aidl文件
            b. 改写自定义的Service,只需改变Service中的那个binder对象,如下:
    public class MyService extends Service{
        private MyBinder binder = new MyBinder();
        继承Stub,也就是实现了ICat接口,并实现了IBinder接口
        public class MyBinder extends Stub {
            public void ggg() {
               
            }
        }


        远程调用端跟上述一样,只是 MyService.MyBinder binder = ICat.Stub.asInterface(service);
  • 相关阅读:
    httpclient的并发连接问题
    Java中使用多线程、curl及代理IP模拟post提交和get访问
    有强大的cURL,忘掉httpclient的吧!
    JavaScript正则表达式在不同浏览器中可能遇到的问题
    Meta对照表
    IntelliJ IDEA 下的svn配置及使用的非常详细的图文总结
    虚拟机centos7服务器下,启动oracle11g数据库和关闭数据库
    Step 4: Install and Configure Databases
    docker 中部署一个springBoot项目
    docker保存对容器的修改
  • 原文地址:https://www.cnblogs.com/cnsec/p/11515787.html
Copyright © 2020-2023  润新知