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

Android的自启动

最近要用到这个,所以也花时间看看。

从分层来说,安卓的自启动也分成三种,app的自启动,framework服务的自启动,HAL服务的自启动。现在简单说说这三种吧。当然,我主要关注的还是最后一种。。。

一 App的自启动

1 AndroidManifest.xml中修改

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

2 编写广播接收器

public class BootCompletedReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            // 启动应用的主活动
            Intent activityIntent = new Intent(context, MainActivity.class);
            activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(activityIntent);

            // 或者启动服务
            Intent serviceIntent = new Intent(context, MyService.class);
            context.startService(serviceIntent);
        }
    }
}

3 在AndroidManifest.xml中注册广播接收器

<receiver android:name=".BootCompletedReceiver" android:enabled="true" android:exported="false">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

二 Framework的自启动

基本上和app差不多,有一些细微修改。

AndroidManifest.xml中定义是这样的。

<service android:name=".MyService" android:enabled="true" android:exported="false" />

在广播接收器中启动服务是这样的。

@Override
public void onReceive(Context context, Intent intent) {
    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
        Intent serviceIntent = new Intent(context, MyService.class);
        context.startService(serviceIntent);
    }
}

三 Hal service的自启动

1 增加service.rc

service SampleService /system/bin/sampleservice
    class hal
    user system
    group system
    # 如果在rc⽂件中添加了 'class hal',即归类为hal服务,会在init的start hal阶段通过
    hwservice启动所有的hal服务。

在Android.bp中增加这个rc文件。

2 增加Selinux权限

关于这部分,可以看看我之前写的:SEAndroid学习12 -- SELinux-CSDN博客

关于这个部分,有两个部分,是一个系统的配置,一个是服务的配置。

系统配置:

在瑞芯微的平台,是这样获取路径的:get_build_var BOARD_SEPOLICY_DIRS

hwservice.te

type vnd_nxpnfc_hwservice, hwservice_manager_type;

hwservice_contexts

vendor.nxp.nxpnfc::INxpNfc (对照manifest中增加的instance,别写错)
u:object_r:vnd_nxpnfc_hwservice:s0

file_contexts

/vendor/bin/hw/vendor\.nxp\.nxpnfc@1\.0-service u:object_r:nxpnfc_hal_exec:s0

服务配置:

fileservice.te

type nxpnfc_hal, domain;
type nxpnfc_hal_exec, exec_type, vendor_file_type, file_type;
init_daemon_domain(nxpnfc_hal)
add_hwservice(nfc, vnd_nxpnfc_hwservice) # 如果是通过nfc进程启动新加的服务,才需要添
加

具体可以参考这个:android 实现一个开机自启动的service_android开机自启动service-CSDN博客

下周会具体做部分工作,到时候再更新把。。。

参考:

Rockchip_Developer_Guide_Android_SELinux(Sepolicy)_CN.pdf

相关文章:

  • 什么是微分和导数?
  • SpringCloud-OpenFeign基础
  • go的context总结
  • 汽车IVI中控开发入门及进阶(二十七):车载摄像头vehicle camera
  • 区块链2024
  • 技术干货|如何快速提升SNMP监控性能
  • 揭秘古代手术工具与技术:从中国起源的医疗奇迹
  • 板凳------56.Linux/Unix 系统编程手册(下) -- SOCKET 介绍
  • [C#] opencvsharp对Mat数据进行序列化或者反序列化以及格式化输出
  • Linux常用命令(17)—pastesortcomm命令(有相关截图)
  • ES6(ECMAScript 6.0) 新特性
  • 如何将 ChatGPT 集成到你的应用中
  • mysql数据库中给表创建数据
  • 靠3个字寻求机会,情商不够,别勉强自己
  • Aquila-Med LLM:开创性的全流程开源医疗语言模型
  • .NET C# 装箱与拆箱
  • TPS、QPS、CPS、PV和UV
  • 深入理解前端缓存
  • LeetCode 1-两数之和
  • 主干网络篇 | YOLOv5/v7 更换主干网络之 ResNet50/ResNet101 | 对比实验必备
  • 构建菌株有效降解有机污染物,上海交大科研成果登上《自然》
  • AI药企英矽智能第三次递表港交所:去年亏损超1700万美元,收入多数来自对外授权
  • 新疆维吾尔自治区乌鲁木齐市米东区政协原副主席朱文智被查
  • 乌克兰议会批准美乌矿产协议
  • 中消协点名新能源汽车行业:定金退款争议频发
  • 明查|这是“C919迫降在农田”?实为飞机模型将用于科普体验