30. Service组件

30. Service组件

30.1 认识Service

服务在后台默默地运行,是不可见的。

30.2 startService与生命周期

新建一个Activity

在这里插入图片描述

在清单文件中设置Activity3默认启动

在这里插入图片描述

MyService.java

package com.dingjiaxiong.myactivitytiaozhuan;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

public class MyService extends Service {

    //服务的生命周期函数


    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("myservice", "onCreate: " );
    }


    //已过期
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.e("myservice", "onStart: " );
    }

    //替代onstart
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("myservice", "onStartCommand: " );
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e("myservice", "onBind: " );
        return null;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("myservice", "onUnbind: " );
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        Log.e("myservice", "onDestroy: " );
        super.onDestroy();
    }
}

在清单文件中注册这个服务

在这里插入图片描述

布局更改

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="startService对应stopService区域"
        android:textSize="20sp"
        android:layout_gravity="center_horizontal"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="startService"
            android:onClick="startService"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="stopService"
            android:onClick="stopService"
            />


    </LinearLayout>


</LinearLayout>

在这里插入图片描述

启动和停止服务

package com.dingjiaxiong.myactivitytiaozhuan;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity3 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
    }

    //启动服务
    public void startService(View view){
        startService(new Intent(this,MyService.class));
    }
    //停止服务
    public void stopService(View view){
        stopService(new Intent(this,MyService.class));
    }
}

运行

在这里插入图片描述

返回桌面,服务不会停止

在这里插入图片描述

点击停止服务按钮后,服务才进行销毁

30.3 bindService与生命周期

布局

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="bindService 对应 unBindService 区域"
    android:textSize="20sp"
    android:layout_gravity="center_horizontal"
    />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bindService"
        android:onClick="bindService"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="unbindService"
        android:onClick="unbindService"
        />

</LinearLayout>

在这里插入图片描述

建立桥梁连接服务

 bind与unbind

public void bindService(View view){

    // connecton 桥梁
    bindService(new Intent(this,MyService.class),connecton, Context.BIND_AUTO_CREATE);
}

public void unbindService(View view){
    unbindService(connecton);
}

//定义桥梁
private ServiceConnection connecton = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

    }
    @Override
    public void onServiceDisconnected(ComponentName componentName) {

    }
};

在这里插入图片描述

点击解绑

在这里插入图片描述

一般的写法,当此activity被销毁时,会自动解绑服务

在这里插入图片描述

posted @ 2022-09-19 08:20  随遇而安==  阅读(14)  评论(0编辑  收藏  举报