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

news2024/9/20 1:06:46

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 30
    buildToolsVersion "30.0.2"
    defaultConfig {
        applicationId "com.lxh.testaidl"
        minSdkVersion 28
        targetSdkVersion 30
        versionCode 1
        def date = new Date().format("yyyyMMddHHmm" , TimeZone.getTimeZone("GMT+08"))
        versionName "testaidl-V1.0-"+date
        testInstrumentationRunner "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 true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility 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";

    @Override
    protected 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() {
        @Override
        public 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;
        }
    };


    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate");
    }

    @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind");
        return asBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "onUnbind");
        return true;
    }

    @Override
    public 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>
    
<service
            android: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";

    @Override
    public 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;
    @Override
    protected 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() {
            @Override
            public 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1006028.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

第 3 章 栈和队列 (使用线性链表和队列实现银行业务模拟)

1. 背景说明 该模拟业务基于时间线来确定&#xff0c;当事件发生时&#xff0c;通过插入升序链表来模拟时间线记录事件发生时间、类型&#xff0c;类似于记事本&#xff0c;由于 时间是单向的&#xff0c;正好符合队列的先进先出特性&#xff0c;类似于我们生活中的排队行为。…

小程序类找茬游戏开发:创造富有挑战性和娱乐性的游戏体验

小程序找茬游戏是一种受欢迎的益智娱乐游戏&#xff0c;玩家需要在两幅几乎相同的图片中找出差异。这种类型的游戏结合了观察力和注意力&#xff0c;提供了有趣的挑战。在本文中&#xff0c;我们将讨论如何开发小程序找茬游戏&#xff0c;以及关键特点和开发流程。 小程序找茬…

初露头角!Walrus入选服贸会“数智影响力”数字化转型创新案例

9月5日&#xff0c;由北京市通信管理局、工业和信息化部新闻宣传中心联合主办的“企业数字化转型论坛”在2023中国国际服务贸易交易会期间召开&#xff0c;论坛以“数字化引领 高质量发展”主题&#xff0c;旨在探讨信息技术如何与各行业深度融合&#xff0c;构建数字化转型新格…

C++vector模拟实现

vector模拟实现 1.构造函数2.拷贝构造3.析构赋值运算符重载4.iterator5.modifiers5.1push_back5.2pop_back5.3empty5.4insert5.5erase5.6swap 6.Capacity6.1size6.2capacity6.3reserve6.4resize6.5empty 7.Element access7.1operator[]7.2at 8.在谈reserve vector官方库实现的是…

SpringBoot整合Redis,基于Jedis实现redis各种操作

前言&#xff08;三步教你学会redis&#xff0c;主打一个实用&#xff09; springboot整合redis步骤&#xff0c;并基于jedis对redis数据库进行相关操作&#xff0c;最后分享非常好用、功能非常全的redis工具类。 第一步&#xff1a;导入maven依赖 <!-- springboot整合re…

C++之智能指针shared_ptr死锁问题(二百)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 人生格言&#xff1a; 人生…

Hadoop生态圈中的Flume数据日志采集工具

Hadoop生态圈中的Flume数据日志采集工具 一、数据采集的问题二、数据采集一般使用的技术三、扩展&#xff1a;通过爬虫技术采集第三方网站数据四、Flume日志采集工具概述五、Flume采集数据的时候&#xff0c;核心是编写Flume的采集脚本xxx.conf六、Flume案例实操1、采集一个网络…

卫星地图-航拍影像-叠加配准套合(ArcGIS版)

卫星地图-航拍影像-叠加配准套合(ArcGIS版) 发布时间&#xff1a;2018-01-17 版权&#xff1a;BIGEMAP 第一步 工具准备 BIGEMAP地图下载器&#xff1a;Bigemap系列产品-GIS行业基础软件kml\shp 相关教程&#xff1a;CAD文件直接导入BIGEMAP进行套合配准&#xff08;推荐&am…

苹果笔不用原装可以吗?苹果ipad触控笔推荐

很多小伙伴在纠结&#xff0c;是否要购买apple pencil呢&#xff1f;但它的价格太过昂贵&#xff0c;很多学生党都消费不起。答案是不一定的要入手apple pencil的。市面上也是有做得相当不错的平替电容笔。现在无纸化已经快成为我们生活中的一部分&#xff0c;它不仅是可以书写…

新网站怎么优化才能提升排名-彻底解决新网站没收录和排名的问题

新网站怎么优化才能提升排名&#xff1f;辛辛苦苦搭建起网站后&#xff0c;却不知如何优化网站提升排名&#xff0c;也没有一个完整的思路和策略&#xff0c;今天做为行业资深SEO的我&#xff0c;来为大家理清SEO思路&#xff0c;彻底解决新网站没收录和排名的问题&#xff01;…

网站不收录没排名降权怎么处理-紧急措施可恢复网站

网站降权对于SEO人员来说是非常致命的打击&#xff0c;因为网站一旦被搜索引擎降权&#xff0c;排名会严重地下降&#xff0c;网站的流量也会大幅下降&#xff0c;直接影响到收益。而且处理不好的话会导致恢复的时间周期无限拉长&#xff0c;所以网站被降权后我们要第一时间采取…

8月客户文章盘点——累计IF 168.4,平均IF 8.42

客户文章一览 凌恩生物以打造国内一流生物公司为目标&#xff0c;在科研测序领域深耕不辍&#xff0c;吸纳多名在生物信息高级技术人员的加盟&#xff0c;参与并完成多个高科技项目。现已在宏组学、基因组、表观遗传以及蛋白代谢等多组学及联合分析领域积累了深厚经验&#xff…

软件测试总结1

1、 什么是软件测试? 答: 软件测试是在规定的条件下对程序进行操作&#xff0c;以发现错误&#xff0c;对软件质量进行评估。 什么是软件测试&#xff1a; 明确地提出了软件测试以检验是否满足需求为目标。 1、保证软件质量的重要手段 预期 ≈ 实际 2、 软件测试的意义 给…

其它机器访问mysql配置

其它机器访问mysql配置 搜索工具 叫 Everything 一、mysql - 改my.ini 刷脚本 也可能叫其他名字 编辑 bind-address0.0.0.0 编辑然后重启一下mysql服务任务管理器-关掉mysql 编辑 搜索 计算机管理-重启mysql服务 编辑 然后 打开查询&#xff0c;并选择mysql数据&#x…

分享微信聊天记录恢复的3个简单方法!

无论身在何处&#xff0c;微信是我们与家人、好友保持紧密联系的好帮手。微信聊天记录不仅仅只是几句话、几张照片……更多的是聊天记录中承载着的美好回忆。如果不小心将聊天记录删除了怎么办&#xff1f;微信聊天记录恢复的方法有哪些&#xff1f;接下来&#xff0c;小编将以…

解决IntelliJ IDEA执行maven打包,执行java -jar命令提示jar中没有主清单属性

问题场景 IDEA执行mvn clean package -DskipTesttrue命令或者借助工具的Maven菜单进行打包操作&#xff0c;然后执行java -jar app.jar命令后&#xff0c;提示jar中没有主清单属性 D:\WorkSpace\demo\target>java -jar demo-SNAPSHOT.jar demo-SNAPSHOT.jar中没有主清单属性…

flex布局中的几个小技巧

1. flex属性一定要写在父级元素的css属性&#xff0c;比如ul里面&#xff0c;不能写到li上面 ul{display:flex; // flex必须写在父元素ul里面justify-content:space-between;height:75px;li{height:75px;line-height:75px;flex:1;padding-left:23px;a{color:$colorB;fo…

3D数字孪生:从3D数据采集到3D内容分析

数字孪生&#xff08;Digital Twin&#xff09;是物理对象、流程或系统的虚拟复制品&#xff0c;用于监控、分析和优化现实世界的对应物。 这些数字孪生在制造、工程和城市规划等领域变得越来越重要&#xff0c;因为它们使我们能够在现实世界中实施改变之前模拟和测试不同的场景…

Linux操作(查询日志)

目录 前言 查看日志 cat less head tail 小结 前言 之前的linux文章属于入门linux,这篇文章主要是linux在后端开发人员中对日志的的运用.对于linux基础掌握不是很好的小伙伴可以先去看看linux基础操作:Linux系统使用(超详细)_linux操作系统使用_陌上 烟雨齐的博客-CSDN博…

Leetcode152. 连续子数组的最大乘积

力扣&#xff08;LeetCode&#xff09;官网 - 全球极客挚爱的技术成长平台 给你一个整数数组 nums &#xff0c;请你找出数组中乘积最大的非空连续子数组&#xff08;该子数组中至少包含一个数字&#xff09;&#xff0c;并返回该子数组所对应的乘积。 测试用例的答案是一个 32…