4.20

所花时间(包括上课):1

打码量(行):100

博客量(篇):1

了解到知识点:学习bindSerview

 

 import android.app.Service;

import android.content.Intent;

import android.os.Binder;

import android.os.IBinder;

import android.util.Log;

import androidx.annotation.Nullable;

 

public class MyBoundService extends Service {

 

    private static final String TAG = "MyBoundService";

    private final IBinder binder = new LocalBinder();

 

    // 提供客户端绑定的接口

    public class LocalBinder extends Binder {

        MyBoundService getService() {

            return MyBoundService.this;

        }

    }

 

    @Override

    public void onCreate() {

        super.onCreate();

        // Service 被创建时调用

        Log.d(TAG, "Service onCreate");

    }

 

    @Nullable

    @Override

    public IBinder onBind(Intent intent) {

        // 当客户端绑定时调用

        Log.d(TAG, "Service onBind");

        return binder;

    }

 

    @Override

    public boolean onUnbind(Intent intent) {

        // 当所有客户端都解绑时调用

        Log.d(TAG, "Service onUnbind");

        return super.onUnbind(intent);

    }

 

    @Override

    public void onDestroy() {

        super.onDestroy();

        // Service 被销毁时调用

        Log.d(TAG, "Service onDestroy");

    }

 

    // 提供给客户端调用的方法

    public String getGreeting() {

        return "Hello from MyBoundService!";

    }

}

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.view.View;

import android.widget.Button;

import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

 

public class MainActivity extends AppCompatActivity {

 

    private MyBoundService myBoundService;

    private boolean isBound = false;

 

    // 服务连接回调

    private ServiceConnection serviceConnection = new ServiceConnection() {

        @Override

        public void onServiceConnected(ComponentName name, IBinder service) {

            // 当服务成功连接时调用

            MyBoundService.LocalBinder binder = (MyBoundService.LocalBinder) service;

            myBoundService = binder.getService();

            isBound = true;

            // 获取服务中的数据

            String greeting = myBoundService.getGreeting();

            TextView textView = findViewById(R.id.text_view);

            textView.setText(greeting);

        }

 

        @Override

        public void onServiceDisconnected(ComponentName name) {

            // 当服务断开连接时调用

            isBound = false;

        }

    };

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        // 获取按钮视图

        Button bindServiceButton = findViewById(R.id.bind_service_button);

        Button unbindServiceButton = findViewById(R.id.unbind_service_button);

 

        // 设置绑定服务按钮点击监听器

        bindServiceButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                // 绑定服务

                Intent intent = new Intent(MainActivity.this, MyBoundService.class);

                bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

            }

        });

 

        // 设置解绑服务按钮点击监听器

        unbindServiceButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                // 解绑服务

                if (isBound) {

                    unbindService(serviceConnection);

                    isBound = false;

                }

            }

        });

    }

}

<!-- activity_main.xml -->

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:padding="16dp">

 

    <!-- 绑定服务按钮 -->

    <Button

        android:id="@+id/bind_service_button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="绑定服务"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="50dp" />

 

    <!-- 解绑服务按钮 -->

    <Button

        android:id="@+id/unbind_service_button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="解绑服务"

        android:layout_centerHorizontal="true"

        android:layout_below="@id/bind_service_button"

        android:layout_marginTop="20dp" />

 

    <!-- 显示服务数据的 TextView -->

    <TextView

        android:id="@+id/text_view"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_below="@id/unbind_service_button"

        android:layout_marginTop="20dp"

        android:text="服务数据" />

 

</RelativeLayout>

posted @ 2024-04-20 19:36  赵千万  阅读(2)  评论(0编辑  收藏  举报