当前位置: 首页 > 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…...

Oracle查询表空间大小

1 查询数据库中所有的表空间以及表空间所占空间的大小 SELECTtablespace_name,sum( bytes ) / 1024 / 1024 FROMdba_data_files GROUP BYtablespace_name; 2 Oracle查询表空间大小及每个表所占空间的大小 SELECTtablespace_name,file_id,file_name,round( bytes / ( 1024 …...

全球首个30米分辨率湿地数据集(2000—2022)

数据简介 今天我们分享的数据是全球30米分辨率湿地数据集&#xff0c;包含8种湿地亚类&#xff0c;该数据以0.5X0.5的瓦片存储&#xff0c;我们整理了所有属于中国的瓦片名称与其对应省份&#xff0c;方便大家研究使用。 该数据集作为全球首个30米分辨率、覆盖2000–2022年时间…...

基础测试工具使用经验

背景 vtune&#xff0c;perf, nsight system等基础测试工具&#xff0c;都是用过的&#xff0c;但是没有记录&#xff0c;都逐渐忘了。所以写这篇博客总结记录一下&#xff0c;只要以后发现新的用法&#xff0c;就记得来编辑补充一下 perf 比较基础的用法&#xff1a; 先改这…...

相机从app启动流程

一、流程框架图 二、具体流程分析 1、得到cameralist和对应的静态信息 目录如下: 重点代码分析: 启动相机前,先要通过getCameraIdList获取camera的个数以及id,然后可以通过getCameraCharacteristics获取对应id camera的capabilities(静态信息)进行一些openCamera前的…...

select、poll、epoll 与 Reactor 模式

在高并发网络编程领域&#xff0c;高效处理大量连接和 I/O 事件是系统性能的关键。select、poll、epoll 作为 I/O 多路复用技术的代表&#xff0c;以及基于它们实现的 Reactor 模式&#xff0c;为开发者提供了强大的工具。本文将深入探讨这些技术的底层原理、优缺点。​ 一、I…...

LRU 缓存机制详解与实现(Java版) + 力扣解决

&#x1f4cc; LRU 缓存机制详解与实现&#xff08;Java版&#xff09; 一、&#x1f4d6; 问题背景 在日常开发中&#xff0c;我们经常会使用 缓存&#xff08;Cache&#xff09; 来提升性能。但由于内存有限&#xff0c;缓存不可能无限增长&#xff0c;于是需要策略决定&am…...

Oracle11g安装包

Oracle 11g安装包 适用于windows系统&#xff0c;64位 下载路径 oracle 11g 安装包...

tauri项目,如何在rust端读取电脑环境变量

如果想在前端通过调用来获取环境变量的值&#xff0c;可以通过标准的依赖&#xff1a; std::env::var(name).ok() 想在前端通过调用来获取&#xff0c;可以写一个command函数&#xff1a; #[tauri::command] pub fn get_env_var(name: String) -> Result<String, Stri…...

jdbc查询mysql数据库时,出现id顺序错误的情况

我在repository中的查询语句如下所示&#xff0c;即传入一个List<intager>的数据&#xff0c;返回这些id的问题列表。但是由于数据库查询时ID列表的顺序与预期不一致&#xff0c;会导致返回的id是从小到大排列的&#xff0c;但我不希望这样。 Query("SELECT NEW com…...

Java中栈的多种实现类详解

Java中栈的多种实现类详解&#xff1a;Stack、LinkedList与ArrayDeque全方位对比 前言一、Stack类——Java最早的栈实现1.1 Stack类简介1.2 常用方法1.3 优缺点分析 二、LinkedList类——灵活的双端链表2.1 LinkedList类简介2.2 常用方法2.3 优缺点分析 三、ArrayDeque类——高…...