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

Android的自启动

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

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

一 App的自启动

1 AndroidManifest.xml中修改

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

2 编写广播接收器

public class BootCompletedReceiver extends BroadcastReceiver {@Overridepublic 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/sampleserviceclass haluser systemgroup 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

http://www.lryc.cn/news/376933.html

相关文章:

  • 开源VisualFbeditor中文版,vb7 IDE,VB6升级64位跨平台开发安卓APP,Linux程序
  • github安全问题token和sshkeys
  • 超详细的selenium使用指南
  • LogicFlow 学习笔记——1. 初步使用 LogicFlow
  • 场外个股期权通道业务是什么意思?
  • 分页插件结合collection标签后分页数量不准确的问题
  • git diff 命令
  • Code Review常用术语
  • HashMap 源码中的巧妙小技巧
  • 极具吸引力的小程序 UI 风格
  • 数据库 | 试卷五试卷六试卷七
  • 网页五子棋对战项目测试(selenium+Junit5)
  • stable diffusion 局部重绘 reference-only api 接口调试
  • 浪潮信息内存故障预警技术再升级 服务器稳定性再获提升
  • JWT整合Gateway实现鉴权(RSA与公私密钥工具类)
  • vue实现全屏screenfull-封装组件
  • 【LinkedList与链表】
  • 为数据安全护航,袋鼠云在数据分类分级上的探索实践
  • Spring 循环依赖详解
  • 项目经理真的不能太“拧巴”
  • 企业如何选择合适的CRM工具?除Salesforce之外的10大主流选择
  • 每年1-1.2万人毕业,男女比例约3:1,测绘工程的就业率如何
  • JimuReport 积木报表 v1.7.6 版本发布,免费的低代码报表
  • “灵活就业者“超两亿人 游戏开发者如何破局?
  • MySQL事务与存储引擎
  • 总是给数据库表字段设置默认值的好处
  • 11.2 Go 常用包介绍
  • Sqlite3数据库基本使用
  • 实现贪吃蛇小游戏【简单版】
  • uniapp实现内嵌其他网页的功能