1
+ /*
2
+ * Copyright 2023-2025 wintmain
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package com.wintmain.foundation.aidl
18
+
19
+ import android.content.ComponentName
20
+ import android.content.Intent
21
+ import android.content.ServiceConnection
22
+ import android.os.Bundle
23
+ import android.os.IBinder
24
+ import android.os.RemoteException
25
+ import android.util.Log
26
+ import androidx.appcompat.app.AppCompatActivity
27
+ import com.google.android.catalog.framework.annotations.Sample
28
+ import lib.wintmain.toaster.toast.ToastUtils
29
+
30
+ @Sample(name = " AIDL-SampleActivity" , description = " AIDL简单示例" , tags = [" A-Self_demos" ])
31
+ class AidlSampleActivity : AppCompatActivity () {
32
+ companion object {
33
+ private const val TAG = " AidlSampleActivity"
34
+ }
35
+ var peopleManager: IPeopleManager ? = null
36
+
37
+ override fun onCreate (savedInstanceState : Bundle ? ) {
38
+ super .onCreate(savedInstanceState)
39
+ // 初始化 Toast
40
+ ToastUtils .init (this .application)
41
+
42
+ // 页面打开的时候就开始绑定服务
43
+ val intent = Intent (this , PeopleRemoteService ::class .java)
44
+ bindService(intent, mConnection, BIND_AUTO_CREATE )
45
+ }
46
+
47
+ override fun onDestroy () {
48
+ // 移除监听
49
+ peopleManager?.let { manager ->
50
+ if (manager.asBinder().isBinderAlive) {
51
+ try {
52
+ manager.unregisterListener(mNewPeopleListener)
53
+ } catch (e: RemoteException ) {
54
+ Log .d(TAG , " catch:" + e.message)
55
+ }
56
+ }
57
+ }
58
+ unbindService(mConnection)
59
+ super .onDestroy()
60
+ }
61
+
62
+ private val mConnection: ServiceConnection = object : ServiceConnection {
63
+ override fun onServiceConnected (className : ComponentName , service : IBinder ) {
64
+ Log .d(TAG , " onServiceConnected..." )
65
+ peopleManager = IPeopleManager .Stub .asInterface(service)
66
+ peopleManager?.run {
67
+ try {
68
+ val people = People (1000 , " wintmain" )
69
+ val book = Book (1 , " Book" )
70
+ // 注册监听
71
+ registerListener(mNewPeopleListener)
72
+ addPeople(people)
73
+ addPeopleAndBook(people, book)
74
+ } catch (e: RemoteException ) {
75
+ Log .d(TAG , " catch:" + e.message)
76
+ }
77
+ }
78
+ }
79
+
80
+ override fun onServiceDisconnected (className : ComponentName ) {
81
+ Log .d(TAG , " onServiceDisconnected!!!" )
82
+ ToastUtils .show(" onServiceDisconnected!!!" )
83
+ peopleManager = null
84
+ }
85
+ }
86
+
87
+ // 定义监听接口
88
+ private val mNewPeopleListener: INewPeopleListener = object : INewPeopleListener .Stub () {
89
+ override fun onNewPeople (newPeople : People ) {
90
+ ToastUtils .show(" onNewPeople: $newPeople " )
91
+ }
92
+ }
93
+ }
0 commit comments