当前位置: 首页 > news >正文

Android Studio的笔记--aidl实现和调用

android AIDL接口使用

  • aidl实现
    • 新建aidl实现工程
      • build.gradle
      • proguard-rules.pro
      • 增加aidl文件
    • 增加aidl实现
      • aidl实现服务
      • 打开aidl服务
  • aidl使用
    • 新建aidl使用工程
      • 增加aidl文件
      • 使用aidl方法
  • 相关回显

aidl实现

新建aidl实现工程

新建一个工程。工程名testaidl。包名com.lxh.testaidl。修改配置文件

build.gradle

a
修改sdk配置,修改version,修改生成apk命名,修改编译混淆配置,增加系统签名
文件位置:\testaidl\app
文件名:build.gradle
在这里插入图片描述

plugins {id 'com.android.application'
}
android {compileSdkVersion 30buildToolsVersion "30.0.2"defaultConfig {applicationId "com.lxh.testaidl"minSdkVersion 28targetSdkVersion 30versionCode 1def date = new Date().format("yyyyMMddHHmm" , TimeZone.getTimeZone("GMT+08"))versionName "testaidl-V1.0-"+datetestInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"}android.applicationVariants.all {variant ->variant.outputs.all {outputFileName = new File(defaultConfig.versionName + ".apk");}}signingConfigs {release {storeFile file("../keystore/mykey.jks")storePassword '123456'keyAlias '_mykey'keyPassword '123456'}debug {storeFile file("../keystore/mykey.jks")storePassword '123456'keyAlias '_mykey'keyPassword '123456'}}buildTypes {release {minifyEnabled trueproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'signingConfig signingConfigs.release}}compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}
}dependencies {implementation 'androidx.appcompat:appcompat:1.2.0'implementation 'com.google.android.material:material:1.2.1'implementation 'androidx.constraintlayout:constraintlayout:2.0.1'testImplementation 'junit:junit:4.+'androidTestImplementation 'androidx.test.ext:junit:1.1.2'androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

b
修改build variants中active build variant 从debug改成release
在这里插入图片描述
c
签名文件
文件位置:\testaidl\keystore
文件名:mykey.jks

proguard-rules.pro

增加混淆
文件位置:\testaidl\app
文件名:proguard-rules.pro

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose
-dontoptimize
-dontpreverify
-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
-keepclasseswithmembernames class * {
native <methods>;
}
-keepclassmembers public class * extends android.view.View {
void set*(***);
*** get*();
}
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keepclassmembers class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator CREATOR;
}
-keepclassmembers class **.R$* {
public static <fields>;
}
-dontwarn android.support.**
-keep class android.support.annotation.Keep
-keep @android.support.annotation.Keep class * {*;}
-keepclasseswithmembers class * {
@android.support.annotation.Keep <methods>;
}
-keepclasseswithmembers class * {
@android.support.annotation.Keep <fields>;
}
-keepclasseswithmembers class * {
@android.support.annotation.Keep <init>(...);
}
-optimizationpasses 5
-dontusemixedcaseclassnames
-ignorewarnings
-keep class com.lxh.testaidl.USTservice { *; }
-keep public class com.lxh.testaidl.aidl.UST {*;}

增加aidl文件

增加aidl文件
文件位置:testaidl\app\src\main\aidl\com\lxh\testaidl\aidl
文件名:UST.aidl

package com.lxh.testaidl.aidl;interface UST {int installPackage(String filepath,int indicator,String version);}

增加aidl实现

编译一遍,能import aidl出来
import com.lxh.testaidl.aidl.UST;

aidl实现服务

新增service,命名USTservice

package com.lxh.testaidl;import android.app.IntentService;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;import androidx.annotation.Nullable;import com.lxh.testaidl.aidl.UST;/*** create by lxh on 2022/12/12 Time:14:35* tip:*/
public class USTservice extends IntentService {private static final String TAG = "USTservice lxh";@Overrideprotected void onHandleIntent(Intent intent) {Log.i(TAG, "onHandleIntent");if (intent != null) {final String action = intent.getAction();}}public TatvUSTservice() {super("USTservice");Log.i(TAG, "USTservice");}public UST.Stub asBinder = new UST.Stub() {@Overridepublic int installPackage(String filepath, int indicator, String version) throws RemoteException {Log.i(TAG, "installPackage(" + filepath + "," + indicator + "," + version);String callerId = getApplicationContext().getPackageManager().getNameForUid(asBinder.getCallingUid());Log.i(TAG, "Calling App:" + callerId);if (callerId.equals("com.lxh.useraidl")) {//在这里放实际的代码}return 1;}};@Overridepublic void onCreate() {super.onCreate();Log.i(TAG, "onCreate");}@Overridepublic int onStartCommand(@Nullable Intent intent, int flags, int startId) {Log.i(TAG, "onStartCommand");return super.onStartCommand(intent, flags, startId);}@Nullable@Overridepublic IBinder onBind(Intent intent) {Log.i(TAG, "onBind");return asBinder;}@Overridepublic boolean onUnbind(Intent intent) {Log.i(TAG, "onUnbind");return true;}@Overridepublic void onDestroy() {Log.i(TAG, "onDestroy");super.onDestroy();}public int installPackage(String filepath, int indicator, String version) {int value = 0;switch (indicator) {case 1:Log.d(TAG, "");value = 1;break;case 2:value = 0;break;default:break;}return value;}
}

清单中

<queries><package android:name="com.tatv.android.TMC.aidl" /></queries><serviceandroid:name=".USTservice"android:enabled="true"android:exported="true"><intent-filter><action android:name="com.lxh.testaidl.aidl.UST" /><category android:name="android.intent.category.DEFAULT" /></intent-filter></service>

打开aidl服务

b新增开机广播接收BootReceiver,用来打开服务

package com.lxh.testaidl;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;/*** create by lxh on 2022/12/12 Time:14:33* tip:*/
public class BootReceiver extends BroadcastReceiver {private static final String TAG = "BootReceiver lxh";@Overridepublic void onReceive(Context context, Intent intent) {Log.i(TAG, "onReceive: " + intent.getAction());if (!TextUtils.isEmpty(intent.getAction()) && intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {Log.i(TAG, "onReceive: ready sent ");Intent it1 = new Intent("com.lxh.testaidl.aidl.UST");it1.setPackage("com.lxh.testaidl");context.startService(it1);}}
}

清单

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /><receiver android:name=".BootReceiver"android:enabled="true"android:exported="true"><intent-filter android:priority="1000"><action android:name="android.intent.action.BOOT_COMPLETED" /></intent-filter></receiver>

aidl使用

新建aidl使用工程

新建一个工程。工程名useraidl。包名com.lxh.useraidl。

增加aidl文件

增加aidl文件
文件位置:useraidl\app\src\main\aidl\com\lxh\testaidl\aidl
文件名:UST.aidl

package com.lxh.testaidl.aidl;interface UST {int installPackage(String filepath,int indicator,String version);}

使用aidl方法

package com.lxh.useraidl;import androidx.appcompat.app.AppCompatActivity;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.util.Log;
import android.view.View;
import android.widget.Button;import com.lxh.testaidl.aidl.UST;
public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity lxh";UST tUST;Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button=findViewById(R.id.button);Intent service = new Intent("com.lxh.testaidl.aidl.UST");service.setPackage("com.lxh.testaidl");bindService(service, connection, Context.BIND_AUTO_CREATE);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {try {tUST.installPackage("1",1,"0");} catch (RemoteException e) {e.printStackTrace();}}});}private ServiceConnection connection = new ServiceConnection(){public void onServiceConnected(ComponentName name, IBinder service){Log.i(TAG, "tUST");tUST = UST.Stub.asInterface(service);}public void onServiceDisconnected(ComponentName name){tUST = null;}};}

相关回显

开机广播:am broadcast -a android.intent.action.BOOT_COMPLETED

2023-09-13 10:22:00.081 15635-15635/com.lxh.testaidl I/BootReceiver lxh: onReceive: android.intent.action.BOOT_COMPLETED
2023-09-13 10:22:00.081 15635-15635/com.lxh.testaidl I/BootReceiver lxh: onReceive: ready sent 
2023-09-13 10:22:00.087 15635-15635/com.lxh.testaidl I/USTservice lxh: USTservice
2023-09-13 10:22:00.092 15635-15635/com.lxh.testaidl I/USTservice lxh: onCreate
2023-09-13 10:22:00.093 15635-15635/com.lxh.testaidl I/USTservice lxh: onStartCommand
2023-09-13 10:22:00.113 15635-15790/com.lxh.testaidl I/USTservice lxh: onHandleIntent
2023-09-13 10:22:00.123 15635-15635/com.lxh.testaidl I/USTservice lxh: onDestroy

运行使用aidl工程

2023-09-13 10:24:07.733 16174-16174/com.lxh.useraidl I/MainActivity lxh: tUST

点击按钮使用aidl方法

2023-09-13 10:24:07.637 15635-15635/com.lxh.testaidl I/USTservice lxh: USTservice
2023-09-13 10:24:07.642 15635-15635/com.lxh.testaidl I/USTservice lxh: onCreate
2023-09-13 10:24:07.643 15635-15635/com.lxh.testaidl I/USTservice lxh: onBind
2023-09-13 10:25:23.618 15635-15667/com.lxh.testaidl I/USTservice lxh: installPackage(1,1,0
2023-09-13 10:25:23.619 15635-15667/com.lxh.testaidl I/USTservice lxh: Calling App:com.lxh.useraidl

相关文章:

Android Studio的笔记--aidl实现和调用

android AIDL接口使用 aidl实现新建aidl实现工程build.gradleproguard-rules.pro增加aidl文件 增加aidl实现aidl实现服务打开aidl服务 aidl使用新建aidl使用工程增加aidl文件使用aidl方法 相关回显 aidl实现 新建aidl实现工程 新建一个工程。工程名testaidl。包名com.lxh.tes…...

大模型从入门到应用——LangChain:代理(Agents)-[工具包(Toolkit)]

分类目录&#xff1a;《大模型从入门到应用》总目录 工具包是工具的集合&#xff0c;这些工具被设计成一起用于特定的任务&#xff0c;并且具有方便的加载方法。常见的工具包如下&#xff1a; CSV代理JiraJSON代理OpenAPI代理自然语言APIPandas数据框架代理PlayWright浏览器工…...

VR全景算不算好的创业项目?有哪些特性?

现在是全民创业的时代&#xff0c;大家都在找创业项目&#xff0c;那么什么是好的创业项目呢&#xff1f;有人会问VR全景算不算创业好项目呢&#xff1f;一般情况下好的创业项目&#xff0c;发展前景和市场消费群体都是比较大的&#xff0c;市场需求大才能满足多数消费者的需求…...

Spring系列文章:Spring集成Log4j2⽇志框架、整合JUnit

一、集成Log4j2⽇志框架 从Spring5之后&#xff0c;Spring框架⽀持集成的⽇志框架是Log4j2.如何启⽤⽇志框架&#xff1a; 第⼀步&#xff1a;引⼊Log4j2的依赖 <!--log4j2的依赖--> <dependency><groupId>org.apache.logging.log4j</groupId><a…...

flink的网络缓冲区

背景 在flink的taskmanager进行数据交互的过程中&#xff0c;网络缓冲区是一个可以提升网络交换速度的设计&#xff0c;此外&#xff0c;flink还通过网络缓冲区实现其基于信用值credit的流量控制&#xff0c;以便尽可能的处理数据倾斜问题 网络缓冲区 在flink中每个taskmana…...

产品经理学习笔记

产品文档之BRD、MRD和PRD - 知乎BRD、MRD和PRD一起被认为是从市场到产品需要形成的标准规范文档&#xff1a; 1、BRD&#xff08;Business Requirement Document&#xff09;&#xff0c;商业需求文档&#xff0c;是一份产品商业论证报告&#xff0c;基于商业目标或价值所描述的…...

【深入理解Linux锁机制】七、互斥体

系列文章: 我的圈子:高级工程师聚集地 【深入理解Linux锁机制】一、内核锁的由来 【深入理解Linux锁机制】二、中断屏蔽 【深入理解Linux锁机制】三、原子操作 【深入理解Linux锁机制】四、自旋锁 【深入理解Linux锁机制】五、衍生自旋锁 【深入理解Linux锁机制】六、信…...

UGUI画布加载优化

在Unity中&#xff0c;UGUI画布的加载优化可以通过以下几种方式来实现&#xff1a; 1. 合理使用画布渲染模式&#xff1a;UGUI画布有三种渲染模式&#xff0c;分别是Screen Space - Overlay、Screen Space - Camera和World Space。在使用时&#xff0c;应根据场景需求选择最适…...

SEC的下一步目标是什么?过时的证券法与加密货币行业,哪个会被先淘汰?

加密货币已经“不合规”了&#xff0c;尤其是其“商业模式”&#xff0c;至少美国证券交易委员会(SEC)主席Gary Gensler这样认为。由于这种观点在美国监管机构中普遍存在&#xff0c;因此涉及加密的执法行动达到历史最高水平也不足为奇。 在短短几年内&#xff0c;我们目睹了所…...

Kafka3.0.0版本——消费者(独立消费者消费某一个主题数据案例__订阅主题)

目录 一、独立消费者消费某一个主题数据案例1.1、案例需求1.2、案例代码1.3、测试 一、独立消费者消费某一个主题数据案例 1.1、案例需求 创建一个独立消费者&#xff0c;消费firstTopic主题中数据&#xff0c;所下图所示&#xff1a; 注意&#xff1a;在消费者 API 代码中必…...

笔记本多拓展出一个屏幕

一、首先要知道&#xff0c;自己的电脑有没有Type-c接口&#xff0c;支持不支持VGA 推荐&#xff1a; 自己不清楚&#xff0c;问客服&#xff0c;勤问。 二、显示屏与笔记本相连&#xff0c;通过VGA 三、连接好了&#xff0c;需要去配置 网址&#xff1a;凑合着看&#xff…...

Redis 高可用及持久化

Redis 高可用 在web服务器中&#xff0c;高可用是指服务器可以正常访问的时间&#xff0c;衡量的标准是在多长时间内可以提供正常服务&#xff08;99.9%、99.99%、99.999%等等&#xff09;。但是在Redis语境中&#xff0c;高可用的含义似乎要宽泛一些&#xff0c;除了保证提供…...

Java高级: 反射

目录 反射反射概述反射获取类的字节码反射获取类的构造器反射获取构造器的作用反射获取成员变量&使用反射获取成员方法反射获取成员方法的作用 反射的应用案例 接下来我们学习的反射、动态代理、注解等知识点&#xff0c;在以后开发中极少用到&#xff0c;这些技术都是以后…...

【计算机网络】什么是WebSocket?

目录 WebSocket简介协议优点使用场景 WebSocket WebSocket是一种网络传输协议,可在单个TCP连接上进行全双工通信&#xff0c;位于OSI模型的应用层。 WebSocket使得客户端和服务器之间的数据交换变得更加简单&#xff0c;允许服务器主动向客户端推送数据。在WebSocket API中&a…...

Apinto 网关: Go语言实现 HTTP 转 gRPC

gRPC 是由 Google 开发的一个高性能、通用的开源RPC框架&#xff0c;主要面向移动应用开发且基于 HTTP/2 协议标准而设计&#xff0c;同时支持大多数流行的编程语言。 gRPC 基于 HTTP/2 协议传输&#xff0c; HTTP/2 相比 HTTP1.x有以下优势: 采用二进制格式传输协议&#xff…...

【管理运筹学】第 7 章 | 图与网络分析(4,最大流问题)

系列文章目录 【管理运筹学】第 7 章 | 图与网络分析&#xff08;1&#xff0c;图论背景以及基本概念、术语、矩阵表示&#xff09; 【管理运筹学】第 7 章 | 图与网络分析&#xff08;2&#xff0c;最小支撑树问题&#xff09; 【管理运筹学】第 7 章 | 图与网络分析&#xf…...

linux学习总结

shell 1.在文本环境下&#xff0c;shell作为命令解释器&#xff0c;建立了用户和操作系统之间的接口。当用户键入一个命令时&#xff0c;shell将对该命令进行解释&#xff0c;并调用相应的程序。2.Linux下有多个shell&#xff0c;最常用的3个shell: bash tcsh zsh3.shell …...

【API 管理】什么是 API 管理,为什么它很重要?

当今复杂的数字生态系统由许多相互关联的部分组成。API 作为看门人和连接器在其中发挥着关键作用——提供了许多最终用户甚至没有注意到的自动化机会和效率。 企业密切关注 API。它们对于应用程序、数据和各种客户交互的功能至关重要。 这使得 API 管理成为几乎每个部门的组织…...

基于人体呼出气体的电子鼻系统的设计与实现

基于人体呼出气体的电子鼻系统的设计与实现 摘要 电子鼻技术是通过模式识别技术对传感器采集的人体呼出气体进行分类训练的方法。本文研究实现的电子鼻系统包括下面几个部分:首先搭建以Arduino为控制核心的气路采集装置&#xff0c;包括MOS传感器和双阀储气袋构建的传感器阵列和…...

OPC发展历程

目录 1 opc 发展历程 1.1 OPC产生的背景 1.2 经典OPC 1.3 OPC UA 2 OPC DA简介 2.1 OPC Server/Client 2.2 OPC Server 2.3 OPC数据更新 2.4 读取数据方式 3 OPC XML简介 3.1 诞生由来 3.2 功能服务 1 opc 发展历程 OPC是英文“OLE for Process Control”的缩写&am…...

(LeetCode 每日一题) 3442. 奇偶频次间的最大差值 I (哈希、字符串)

题目&#xff1a;3442. 奇偶频次间的最大差值 I 思路 &#xff1a;哈希&#xff0c;时间复杂度0(n)。 用哈希表来记录每个字符串中字符的分布情况&#xff0c;哈希表这里用数组即可实现。 C版本&#xff1a; class Solution { public:int maxDifference(string s) {int a[26]…...

Cursor实现用excel数据填充word模版的方法

cursor主页&#xff1a;https://www.cursor.com/ 任务目标&#xff1a;把excel格式的数据里的单元格&#xff0c;按照某一个固定模版填充到word中 文章目录 注意事项逐步生成程序1. 确定格式2. 调试程序 注意事项 直接给一个excel文件和最终呈现的word文件的示例&#xff0c;…...

Objective-C常用命名规范总结

【OC】常用命名规范总结 文章目录 【OC】常用命名规范总结1.类名&#xff08;Class Name)2.协议名&#xff08;Protocol Name)3.方法名&#xff08;Method Name)4.属性名&#xff08;Property Name&#xff09;5.局部变量/实例变量&#xff08;Local / Instance Variables&…...

《用户共鸣指数(E)驱动品牌大模型种草:如何抢占大模型搜索结果情感高地》

在注意力分散、内容高度同质化的时代&#xff0c;情感连接已成为品牌破圈的关键通道。我们在服务大量品牌客户的过程中发现&#xff0c;消费者对内容的“有感”程度&#xff0c;正日益成为影响品牌传播效率与转化率的核心变量。在生成式AI驱动的内容生成与推荐环境中&#xff0…...

2025 后端自学UNIAPP【项目实战:旅游项目】6、我的收藏页面

代码框架视图 1、先添加一个获取收藏景点的列表请求 【在文件my_api.js文件中添加】 // 引入公共的请求封装 import http from ./my_http.js// 登录接口&#xff08;适配服务端返回 Token&#xff09; export const login async (code, avatar) > {const res await http…...

基于matlab策略迭代和值迭代法的动态规划

经典的基于策略迭代和值迭代法的动态规划matlab代码&#xff0c;实现机器人的最优运输 Dynamic-Programming-master/Environment.pdf , 104724 Dynamic-Programming-master/README.md , 506 Dynamic-Programming-master/generalizedPolicyIteration.m , 1970 Dynamic-Programm…...

安宝特方案丨船舶智造的“AR+AI+作业标准化管理解决方案”(装配)

船舶制造装配管理现状&#xff1a;装配工作依赖人工经验&#xff0c;装配工人凭借长期实践积累的操作技巧完成零部件组装。企业通常制定了装配作业指导书&#xff0c;但在实际执行中&#xff0c;工人对指导书的理解和遵循程度参差不齐。 船舶装配过程中的挑战与需求 挑战 (1…...

C++.OpenGL (14/64)多光源(Multiple Lights)

多光源(Multiple Lights) 多光源渲染技术概览 #mermaid-svg-3L5e5gGn76TNh7Lq {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-3L5e5gGn76TNh7Lq .error-icon{fill:#552222;}#mermaid-svg-3L5e5gGn76TNh7Lq .erro…...

关于uniapp展示PDF的解决方案

在 UniApp 的 H5 环境中使用 pdf-vue3 组件可以实现完整的 PDF 预览功能。以下是详细实现步骤和注意事项&#xff1a; 一、安装依赖 安装 pdf-vue3 和 PDF.js 核心库&#xff1a; npm install pdf-vue3 pdfjs-dist二、基本使用示例 <template><view class"con…...

Unity UGUI Button事件流程

场景结构 测试代码 public class TestBtn : MonoBehaviour {void Start(){var btn GetComponent<Button>();btn.onClick.AddListener(OnClick);}private void OnClick(){Debug.Log("666");}}当添加事件时 // 实例化一个ButtonClickedEvent的事件 [Formerl…...