Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app-catalog/samples/foundation/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
apply from: "$rootDir/gradle/sample-build.gradle"

apply plugin: 'kotlin-parcelize'

android {
sourceSets {
main {
Expand Down
12 changes: 11 additions & 1 deletion app-catalog/samples/foundation/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,20 @@
</receiver>

<service
android:name=".PeopleRemoteService"
android:name=".aidl.PeopleRemoteService"
android:process=":remote"
android:enabled="true"
android:exported="true" />

<activity
android:name=".aidl.AidlSampleActivity"
android:exported="true"
android:theme="@style/Theme.AppCompat.DayNight" />

<activity
android:name=".webview.CustomActionWebViewActivity"
android:exported="true"
android:theme="@style/Theme.AppCompat.DayNight" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.wintmain.foundation.aidl;

import com.wintmain.foundation.aidl.Book;

parcelable Book;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// INewPeopleListener.aidl
package com.wintmain.foundation;
package com.wintmain.foundation.aidl;

import com.wintmain.foundation.People;
import com.wintmain.foundation.aidl.People;

// AIDL中无法使用普通接口,所以创建此文件

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// IPeopleManager.aidl
// AIDL接口中只支持方法,不支持声明静态常量
package com.wintmain.foundation;
package com.wintmain.foundation.aidl;

// 自定义的Parcelable对象和AIDL对象不管是否和当前的AIDL文件位于同一个包内,也必须要显式import进来

import com.wintmain.foundation.People;
import com.wintmain.foundation.INewPeopleListener;
import com.wintmain.foundation.aidl.Book;
import com.wintmain.foundation.aidl.People;
import com.wintmain.foundation.aidl.INewPeopleListener;

interface IPeopleManager {
List<People> getPeopleList();
// addPeople方法的参数中有in关键字,因为aidl中除了基本数据类型,其它类型的参数必须标上方向:in、out或者inout,
// 我们需根据实现需要去指定参数类型,因为这在底层实现是有开销的。
void addPeople(in People people);

void addPeopleAndBook(in People people, in Book book);

// 扩展新增代码:新增接口
void registerListener(INewPeopleListener listener);
void unregisterListener(INewPeopleListener listener);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// People.aidl
package com.wintmain.foundation;
package com.wintmain.foundation.aidl;

// Declare any non-default types here with import statements

Expand All @@ -14,7 +14,7 @@ package com.wintmain.foundation;
// Parcelable的作用是序列化对象,因为跨进程通信传输对象必须是以序列化和反序列化的方式进行。
// Parcelable的实现有些繁琐,但性能效率很高。

import com.wintmain.foundation.People;
import com.wintmain.foundation.aidl.People;

// People should be declared in a file called com/wintmain/foundation/People.aidl
// People should be declared in a file called com/wintmain/foundation/aidl/People.aidl
parcelable People;
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
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.os.RemoteException;
import android.provider.AlarmClock;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
Expand All @@ -39,19 +36,12 @@
description = "一个为了测试API调用的入口",
tags = "A-Self_demos")
public class PlaceHolderActivity extends AppCompatActivity {
private static final String TAG = PlaceHolderActivity.class.getSimpleName();
IPeopleManager peopleManager;

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

// AIDL示例 S
Intent intent = new Intent(this, PeopleRemoteService.class);
bindService(intent, mConnection, BIND_AUTO_CREATE);
// AIDL示例 E

// 初始化一些三方库
initLibs(getApplication());

Expand Down Expand Up @@ -87,20 +77,6 @@ public void onRightClick(TitleBarExt titleBar) {
});
}

@Override
protected void onDestroy() {
// 移除监听
if (peopleManager != null && peopleManager.asBinder().isBinderAlive()) {
try {
peopleManager.unregisterListener(mNewPeopleListener);
} catch (RemoteException e) {
android.util.Log.d(TAG, "catch:" + e.getMessage());
}
}
unbindService(mConnection);
super.onDestroy();
}

private void initLibs(Application application) {
// 初始化 TitleBar 默认样式
TitleBarExt.setDefaultStyle(
Expand All @@ -124,34 +100,4 @@ public TextView newRightView(Context context) {
// 初始化 Toast
ToastUtils.init(this.getApplication());
}


private final ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
android.util.Log.d(TAG, "onServiceConnected...");
peopleManager = IPeopleManager.Stub.asInterface(service);
try {
People people = new People(1000, "wintmain");
// 注册监听
peopleManager.registerListener(mNewPeopleListener);
peopleManager.addPeople(people);
} catch (RemoteException e) {
android.util.Log.d(TAG, "catch:" + e.getMessage());
}
}
public void onServiceDisconnected(ComponentName className) {
android.util.Log.d(TAG, "onServiceDisconnected!!!");
ToastUtils.show("onServiceDisconnected!!!");
peopleManager = null;
}
};


// 定义监听接口
private final INewPeopleListener mNewPeopleListener = new INewPeopleListener.Stub() {
@Override
public void onNewPeople(People newPeople) {
ToastUtils.show("onNewPeople: " + newPeople);
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2023-2025 wintmain
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.wintmain.foundation.aidl

import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.os.RemoteException
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.google.android.catalog.framework.annotations.Sample
import lib.wintmain.toaster.toast.ToastUtils

@Sample(name = "AIDL-SampleActivity", description = "AIDL简单示例", tags =["A-Self_demos"])
class AidlSampleActivity: AppCompatActivity() {
companion object {
private const val TAG = "AidlSampleActivity"
}
var peopleManager: IPeopleManager? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 初始化 Toast
ToastUtils.init(this.application)

// 页面打开的时候就开始绑定服务
val intent = Intent(this, PeopleRemoteService::class.java)
bindService(intent, mConnection, BIND_AUTO_CREATE)
}

override fun onDestroy() {
// 移除监听
peopleManager?.let { manager ->
if (manager.asBinder().isBinderAlive) {
try {
manager.unregisterListener(mNewPeopleListener)
} catch (e: RemoteException) {
Log.d(TAG, "catch:" + e.message)
}
}
}
unbindService(mConnection)
super.onDestroy()
}

private val mConnection: ServiceConnection = object : ServiceConnection {
override fun onServiceConnected(className: ComponentName, service: IBinder) {
Log.d(TAG, "onServiceConnected...")
peopleManager = IPeopleManager.Stub.asInterface(service)
peopleManager?.run {
try {
val people = People(1000, "wintmain")
val book = Book(1, "Book")
// 注册监听
registerListener(mNewPeopleListener)
addPeople(people)
addPeopleAndBook(people, book)
} catch (e: RemoteException) {
Log.d(TAG, "catch:" + e.message)
}
}
}

override fun onServiceDisconnected(className: ComponentName) {
Log.d(TAG, "onServiceDisconnected!!!")
ToastUtils.show("onServiceDisconnected!!!")
peopleManager = null
}
}

// 定义监听接口
private val mNewPeopleListener: INewPeopleListener = object : INewPeopleListener.Stub() {
override fun onNewPeople(newPeople: People) {
ToastUtils.show("onNewPeople: $newPeople")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2023-2025 wintmain
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.wintmain.foundation.aidl

import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
data class Book(
val pId: Int,
val pName: String
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.wintmain.foundation;
package com.wintmain.foundation.aidl;

import android.os.Parcel;
import android.os.Parcelable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
* limitations under the License.
*/

package com.wintmain.foundation
package com.wintmain.foundation.aidl

import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import android.os.RemoteCallbackList
import android.util.Log
import java.util.concurrent.CopyOnWriteArrayList

// 服务端代码,客户端见[PlaceHoldActivity.java]
// 服务端代码,客户端见[AidlSampleActivity.java]
class PeopleRemoteService : Service() {
val mPeopleList = CopyOnWriteArrayList<People>()
// 客户端注册和移除注册过程中使用的虽是同一个客户端对象,
Expand All @@ -47,6 +48,10 @@ class PeopleRemoteService : Service() {
mListenList.finishBroadcast()
}

override fun addPeopleAndBook(people: People, book: Book) {
Log.d("wintmain", "addPeopleAndBook: people = $people, book = $book")
}

// RemoteCallbackList是系统专门提供的用于删除跨进程listener接口,它是一个泛型,支持管理任意AIDL接口。
// 另外它内部自动实现了线程同步的功能。
// beginBroadcast和finishBroadcast必须要配对使用,哪怕仅仅获取RemoteCallbackList中的元素个数
Expand Down
Loading
Loading