• 測试Service


    <strong><span style="font-size:18px;">自己定义Service:</span></strong>
    /**
     * Created by liuwang on 15-3-15.
     */
    public class MyService extends Service {
    
        private MBinder mBinder = new MBinder();
    
        public MyService() {
            super();
            System.out.println("MyService---");
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            System.out.println("onBind---");
            return mBinder;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            System.out.println("onCreate---");
        }
    
        @Override
        public void onStart(Intent intent, int startId) {
            super.onStart(intent, startId);
            System.out.println("onStart---");
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            System.out.println("onStartCommand---");
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        public boolean onUnbind(Intent intent) {
            System.out.println("onUnbind---");
            return super.onUnbind(intent);
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            System.out.println("onDestroy---");
        }
    
    
    
        @Override
        public void onRebind(Intent intent) {
            super.onRebind(intent);
            System.out.println("onRebind---");
        }
    
        @Override
        public void onTaskRemoved(Intent rootIntent) {
            super.onTaskRemoved(rootIntent);
            System.out.println("onTaskRemoved---");
        }
    
        @Override
        protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
            super.dump(fd, writer, args);
            System.out.println("dump---");
        }
    
        @Override
        public void onTrimMemory(int level) {
            super.onTrimMemory(level);
            System.out.println("onTrimMemory---");
        }
    
        @Override
        public void onLowMemory() {
            super.onLowMemory();
            System.out.println("onLowMemory---");
        }
    
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            System.out.println("onConfigurationChanged---");
        }
    
        class MBinder extends Binder {
    
        }
    
    }
    Activity:
    public class MainActivity extends ActionBarActivity implements View.OnClickListener {
    
        private Button start;
        private Button finish;
        private Button onbind;
        private Button unbind;
        private Intent intent;
        private ServiceConnection sc;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            start = (Button) findViewById(R.id.start);
            finish = (Button) findViewById(R.id.finish);
            onbind = (Button) findViewById(R.id.onbind);
            unbind = (Button) findViewById(R.id.unbind);
    
            start.setOnClickListener(this);
            finish.setOnClickListener(this);
            onbind.setOnClickListener(this);
            unbind.setOnClickListener(this);
    
            intent = new Intent();
            intent.setClass(this,MyService.class);
    
            sc = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    System.out.println("连接成功");
                }
    
                @Override
                public void onServiceDisconnected(ComponentName name) {
                    System.out.println("连接失败");
                }
            };
    
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.start:
                    startService(intent);
                    break;
                case R.id.finish:
                    stopService(intent);
                    break;
                case R.id.onbind:
                    bindService(intent,sc,BIND_AUTO_CREATE);
                    break;
                case R.id.unbind:
                    unbindService(sc);
    
                    break;
            }
        }
    }

    測试1:

    当点击启动Service时的流程:


    在点击绑定:

    点击解绑:

    最后点击关闭服务:


    測试2:

    点击绑定:

    点击启动:

    点击关闭服务:

    无输出。

    点击解除绑定:


    总结:

    当我进行启动服务和绑定服务后,假设仅仅是关闭服务或解绑服务并不会将service杀死。

    当我进行启动服务后在绑定。并不会反复创建service。

    先启动在绑定的流程:构造方法->oncreate->onstartcommand->onstart->onbind->onserviceconnection

    先绑定service在启动service流程:构造方法->oncreate->onbind->onserviceconnection->onstartcommand->onstart

    当我反复启动service仅仅会反复调用startcommand和start方法,不会反复调用oncreate

    当我进行多次绑定时并不会反复创建多个service

    当我绑定service而且退出activity不解绑service时,service会自己主动被关闭,而且logcat会报错

    我能够利用绑定service后启动service然后退出activity解绑service,这样service还不会被kill。再次进入activity我们能够通过关闭service使service关闭。


  • 相关阅读:
    react之redux的使用笔记
    react之jsx的使用
    react之第一个组件的定义及使用
    npm
    webpack热加载
    react使用笔记及生命周期
    移动开发的常见问题
    javascript常用的方法
    cordova local notification plugin
    jqmobi 转换语言
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/6703415.html
Copyright © 2020-2023  润新知