自定义服务类

自定义服务类

  • Service是一个长期运行在后台的用户组建,没有用户界面。即使切换到另一个应用程序,服务也可以在后台运行,因此服务更适合执行一段时间而又不需要显示界面的后台操作。

  • 服务的创建
    在这里插入图片描述

  • 服务的生命周期

    • onCreate():第一次创建服务时执行的方法;
    • onDestory():服务被销毁时执行的方法;
    • onStartCommon():客户端通过调用startService()方法启动服务是执行该方法;
    • onBind():客户端通过调用bindService()方法启动服务时执行该方法;
    • onUnbind():客户端调用unBindService()断开服务绑定时执行的方法。
      在这里插入图片描述
      从图中可以看出,当通过startService()方法启动服务时,执行的生命周期方法为onCreate()、onStartCommand(),然后服务处于运行状态,直到自身调用stopSelf()方法或者其他组件调用stopService()方法时停止服务,最终被系统销毁。当使用bindService()方法启动服务的时候,执行的生命周期方法为onCreate()、onBind(),然后服务处于运行状态,直到调用unBindService()方法时,服务被解绑调用onUnbind()方法,最终被销毁。
  • 1.新建项目“Service”,项目中包含:

    • 一个MainActivity,其布局有四个按钮:”start” “stop” “bind” “unbind”;
  • 2.给项目增加一个自定义服务MyService, 请重写它的回调方法: onCreate(), onStartCommand(), onBind(), onUnbind(), onDestroy(),使得它们在被执行时打印一条Log信息, 显示格式为“MyService – 回调方法名”;

  • 3.编写MainActivity的四个按钮的单击监听事件,分别调用startService(), stopService(), bindService(), unbindService()方法

  • 4.运行与Logcat窗口截图:

    • 1)单击start按钮,然后单击stop按钮 ;
      在这里插入图片描述
    • 2)单击bind按钮,然后单击unbind按钮 ;
      在这里插入图片描述
    • 3)单击start按钮, 然后单击unbind按钮 ;
      在这里插入图片描述
    • 4)单击bind按钮,然后单击stop按钮 ;
      在这里插入图片描述
    • 5)单击start按钮,然后单击回退按钮退出MainActivity ;
      在这里插入图片描述
    • 6)单击bind按钮,然后单击回退按钮退出MainActivity 。
      在这里插入图片描述
  • activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:text="start"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.062" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:text="stop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.48"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button"
            app:layout_constraintVertical_bias="0.082" />
    
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:text="bind"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.492"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button2"
            app:layout_constraintVertical_bias="0.115" />
    
        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:text="unbind"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.502"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button3"
            app:layout_constraintVertical_bias="0.185" />
    
        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:text="SHOW COUNT"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button4" />
    
    </android.support.constraint.ConstraintLayout>
    
  • MainActivity.java

    package com.example.customservice;
    
    import android.content.ComponentName;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.IBinder;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    //两种服务类要相互对应,start countservice与stop countservice之间与mainActivity是没有关联的;
    // 但是bind countservice与unbind countservice之间与mainActivity是有关联的;所以这一对中mainActivity关闭了就会直接ondestroy()
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
        private Button btn1,btn2,btn3,btn4,btn5;
        final static String TAG = "MainActivity";
        private ServiceConnection serviceConnection;
        private MyService.CountBinder binder;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            btn1 = findViewById(R.id.button);
            btn2 = findViewById(R.id.button2);
            btn3 = findViewById(R.id.button3);
            btn4 = findViewById(R.id.button4);
            btn5 = findViewById(R.id.button5);
    
            btn1.setOnClickListener(this);
            btn2.setOnClickListener(this);
            btn3.setOnClickListener(this);
            btn4.setOnClickListener(this);
            btn5.setOnClickListener(this);
    
            serviceConnection = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    binder = (MyService.CountBinder)service;
                }
    
                @Override
                public void onServiceDisconnected(ComponentName name) {
    
                }
            };
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.button://start countservice
                    Intent service = new Intent(MainActivity.this,MyService.class);
                    startService(service);
                    Log.i(TAG,"Start MyService");
                    break;
                case R.id.button2://stop countservice
                    service = new Intent(MainActivity.this,MyService.class);
                    stopService(service);
                    Log.i(TAG,"Stop MyService");
                    break;
                case R.id.button3://bind countservice
                    service = new Intent(MainActivity.this,MyService.class);
                    bindService(service,serviceConnection,BIND_AUTO_CREATE);
                    Log.i(TAG,"Bind MyService");
                    break;
                case R.id.button4://unbind countservice
                    unbindService(serviceConnection);
                    Log.i(TAG,"Unbind MyService");
                    break;
                case R.id.button5://show count
                    Log.i(TAG,"count=" + binder.getCount());
                    break;
            }
        }
    }
    
    
  • MyService.java

    package com.example.customservice;
    
    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 {//从0开始,每隔一秒计数一次
    
        final static String TAG="service";
        private int count;
        private boolean isCount;
    
        public MyService() {//构造方法
        }
    
        public class CountBinder extends Binder{
            public int getCount(){
                return count;
            }
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            //throw new UnsupportedOperationException("Not yet implemented");
            Log.i(TAG,"onBind");
            return new CountBinder();
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            Log.i(TAG,"MyService--onCreate()");
    
            count = 0;
            isCount = true;
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (isCount){
                        //rest 1000ms
                        try{
                            Thread.sleep(1000);
                        }
                        catch (InterruptedException e){
                            e.printStackTrace();
                        }
                        //count++
                        count++;
                    }
                }
            }).start();
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.i(TAG,"MyService--onStartCommand()");
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        public boolean onUnbind(Intent intent) {
            Log.i(TAG,"MyService--onUnbind()");
            return super.onUnbind(intent);
        }
    
        @Override
        public void onDestroy() {
            isCount = false;
            super.onDestroy();
            Log.i(TAG,"MyService--OnDestroy()");
        }
    }
    
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值