Android四大组件--Service

2019-06-17  本文已影响0人  木木不哭_8be4

Android四大组件--Service

1.简单的创建和停止

MainActity.java

public class MainActivity extends AppCompatActivity {
    //三个界面按钮
    Button btnStart;
    Button btnStop;
    Button btnGet;
    //初始化
    int number=0;
    
    Intent intent =new Intent();
    //实现了binder接口的实例
    Service01.MyBind myBind;
    
    ServiceConnection serviceConnection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBind=(Service01.MyBind) service;
            number=myBind.getNum();
            Log.e("haha", "onServiceConnected: "+number );
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    void init(){
        btnStart=findViewById(R.id.btn_start);
        btnStop=findViewById(R.id.btn_stop);
        btnGet=findViewById(R.id.btn_get);
        btnStart.setOnClickListener(new BtnListener());
        btnStop.setOnClickListener(new BtnListener());
        btnGet.setOnClickListener(new BtnListener());
        //指定Service
        intent.setClass(MainActivity.this, Service01.class);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化
        init();
    }
    class BtnListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_start:
                    //Intent service, ServiceConnection conn,int flags
                    bindService(intent,serviceConnection,BIND_AUTO_CREATE);
                    break;
                case R.id.btn_stop:
                    unbindService(serviceConnection);
                    Log.e("haha", "unbindService(serviceConnection);: " );
                    break;
                case R.id.btn_get:
                    Log.e("haha", "onServiceConnected: "+number );
                    break;
                default:
                        break;
            }
        }
    }
}

Service01.java

public class Service01 extends Service {
    private int num=0;
    MyBind myBind=new MyBind();
    public Service01() {
        super();
    }

    @Override
    public void onCreate() {
        super.onCreate();

        Log.e("haha", "onCreate: Service01创建了" );
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("haha", "onDestroy: Service01销毁了" );
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {

        return myBind;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("haha", "onUnbind: Service01onUnbind了" );
        return super.onUnbind(intent);
    }
    public class MyBind extends Binder{
        public int getNum(){
            return num+10;
        }
    }
}

AndroidMainfest.xml

...
<!--注册服务-->
        <service android:name=".service.Service01"></service>
...
上一篇下一篇

猜你喜欢

热点阅读