• android 47 service绑定


    如果一个service已经启动了,activity和service绑定了在解除邦定,则这个service不会销毁,因为这个service不是这个Activity创建的。

     service生命周期:

    Activity绑定的同时创建service则解除绑定的时候service销毁。

    main.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/btnStartService"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="start service" />
    
        <Button
            android:id="@+id/btnBindService"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="bind service" />
    
        <Button
            android:id="@+id/btnUnbindService"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="unBind service" />
    
    </LinearLayout>

    mainACtivity.java

    package com.sxt.day07_03;
    
    import com.sxt.day07_03.MyService.MyBinder;
    
    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.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    
    public class MainActivity extends Activity {
        MyBinder mBider;
        Intent mIntent;
        private ServiceConnection conn=new ServiceConnection() {//conn是接口类型
            @Override
            //绑定后由于异常被迫解除绑定时调用。
            public void onServiceDisconnected(ComponentName name) {
            }
            @Override
            //绑定成功Service的onBind方法执行后调用,已经绑定成功再绑定是不会调用这个方法的,并接收onBind方法的返回值(传过来的是地址是同一个对象,要实现IBinder接口)
            public void onServiceConnected(ComponentName name, IBinder service) {
                mBider=(MyBinder) service;
                Log.i("main",service.toString()+",count:"+mBider.getCount());//返回11
            }
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            setListener();
        }
    
        private void setListener() {
            setStartServiceClickListener();
            bindServiceClickListener();
            unBindServiceClickListener();
        }
    
        private void unBindServiceClickListener() {
            findViewById(R.id.btnUnbindService).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mIntent==null){
                        return ;
                    }
                    unbindService(conn);
                }
            });
        }
    
        private void bindServiceClickListener() {
            findViewById(R.id.btnBindService).setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    mIntent=new Intent(MainActivity.this, MyService.class);
                    bindService(mIntent, conn, Context.BIND_AUTO_CREATE);//conn是绑定成功时候回调的接口的实现类,BIND_AUTO_CREATE表示如果没有启动则连绑定再启动。
                }
            });
        }
    
        private void setStartServiceClickListener() {
            findViewById(R.id.btnStartService).setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    mIntent=new Intent(MainActivity.this, MyService.class);
                    startService(mIntent);
                }
            });
        }
    
    }

    service.java

    package com.sxt.day07_03;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    import android.util.Log;
    
    public class MyService extends Service {
        private int mCount=10;
        @Override
        //绑定成功的时候调用,已经绑定成功再绑定是不会调用这个方法的。
        public IBinder onBind(Intent intent) {
            MyBinder binder=new MyBinder();
            return binder;
        }
        class MyBinder extends Binder{//这里继承Binder类而不是IBinder接口,因为继承接口会重写方法,是不必要的。
            public int getCount(){
                return ++mCount;
            }
        }
        @Override
        public void onCreate() {//创建的时候调用,先创建再绑定。
            super.onCreate();
            Log.i("main","onCreate()");
        }
        
        @Override
        public boolean onUnbind(Intent intent) {
            Log.i("main","onUnbind");
            return true;//想解除绑定后再绑定则返回true,onUnbind方法执行后并执行onRebind方法
        }
        
        @Override
        public void onRebind(Intent intent) {//解除绑定后再绑定执行这个方法
            super.onRebind(intent);
            Log.i("main","onRebind()");
        }
        
        @Override
        public void onDestroy() {//如果这个Activity创建的这个service,则解绑的时候会调用onDestroy方法,如果不是这个Activity创建的则解绑的时候不会销毁这个service,因为不是他创建的。
            super.onDestroy();
            Log.i("main","onDestroy()");
        }
    }

     安卓的4大组件在自定义的时候都要在说明文件声明.

    说明文件要添加:<service android:name="com.sxt.day07_03.MyService"/>

  • 相关阅读:
    java中的HMAC-SHA1加密
    java拦截处理System.exit(0)
    使用canal分析binlog(二) canal源码分析
    JS的异步世界
    socket.io的用户认证
    一个补零小函数
    使用gulp在开发过程中合理导出zip文件
    使用r.js进行前端repuirejs的合并压缩
    使用r2d3的注意事项
    三列自适应布局的实现方式(兼容IE6+)
  • 原文地址:https://www.cnblogs.com/yaowen/p/4890685.html
Copyright © 2020-2023  润新知