• 以绑定的方式来启动service


    先说下原理,之前我们的启动service就是用startService来启动的,这是显式启动。启动后我们无法得到service中的数据,也无法知道它执行的状态,如果我们要启动它的activity和它建立一个联系,获得他的数据或者是执行其内部的方法时就需要隐式启动了。

    关键原理在于使用一个binder来传递数据,并且要给service配置一个action作为标签。

    BindService

    package com.example.service;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    import android.util.Log;
    
    public class BindService extends Service{
        
        String tag = getClass().getSimpleName();
    
        private int count = 123;
        private MyBinder binder = new MyBinder();
        
        //建立一个binder对象,用来在onbind中返回
        public class MyBinder extends Binder{
            public int getCount() {
                return count;
            }
        }
        
        /* (非 Javadoc)
         * @see android.app.Service#onBind(android.content.Intent)
         * 连接时执行的方法
         */
        @Override
        public IBinder onBind(Intent intent) {
            // TODO 自动生成的方法存根
            Log.i(tag, "onBind");
            return binder;
        }
        
        /* (非 Javadoc)
         * @see android.app.Service#onUnbind(android.content.Intent)
         * 断开连接时回调方法
         */
        @Override
        public boolean onUnbind(Intent intent) {
            // TODO 自动生成的方法存根
            Log.i(tag, "onUnbind");
            return super.onUnbind(intent);
        }
        
        @Override
        public void onCreate() {
            // TODO 自动生成的方法存根
            super.onCreate();
            Log.i(tag, "onCreat");
        }
        
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // TODO 自动生成的方法存根
            Log.i(tag, "onStartCommand");
            return super.onStartCommand(intent, flags, startId);
        }
        
        @Override
        public void onDestroy() {
            // TODO 自动生成的方法存根
            super.onDestroy();
            Log.i(tag, "onDestroy");
        }
    
    
    
    }

    这里binder就可以通过其内部的getcount方法来向外部传递数据了。接下来要配置service了,这里的action就是用来给启动时作为标记的。

     <service android:name="com.example.service.BindService" >
                <intent-filter>
                    <action android:name="com.example.service.BindService" />
                </intent-filter>
    </service>

    最后是在activity中通过button来启动service

    package com.example.service;
    
    import android.app.Activity;
    import android.app.Service;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
        private BindService.MyBinder binder;
        private ServiceConnection connection = new ServiceConnection() {
            
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                //连接成功时,获取service的binder对象
                binder = (BindService.MyBinder)service;
            }
            
            @Override
            public void onServiceDisconnected(ComponentName name) {
                // TODO 断开连接时
            }
        };
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
        }
        
        public void buttonListener(View v) {
            //设置action,这样启动的时候就知道是启动哪个service了
            Intent intent = new Intent("com.example.service.BindService");
            switch (v.getId()) {
            case R.id.binderService_button:
                //通过这个过滤器来判断绑定哪个service,所以在配置的时候需要配置action
                bindService(intent, connection, Service.BIND_AUTO_CREATE);
                break;
            case R.id.unbinderService_button:
                unbindService(connection);
                break;
            case R.id.getData_button:
                System.out.println("service 中的数据 = :"+ binder.getCount());
                break;
            default:
                break;
            }
        }
    }
  • 相关阅读:
    vs中添加wsdl生成代理类工具
    vscode+prettier 设置保存自动格式化
    k8s 部署项目
    Jmate使用
    k8s部署项目
    docker 打包镜像 部署项目
    vs2012编译xp运行的mfc程序InitializeCriticalSectionEx解决方案
    thinkphp 入口文件 iis 500错误
    java初学之stream
    php preg_match正则长度限制
  • 原文地址:https://www.cnblogs.com/tianzhijiexian/p/3980683.html
Copyright © 2020-2023  润新知