Android 系统添加开机自启动脚本(转)

本文转载链接:Android 系统添加开机自启动脚本_我的小鱼楠的博客-CSDN博客_安卓开机自启脚本

OS:Android8.1
Platform:PX30
我们这儿以创建一个开机自启动日志服务为例

1、创建需要自启动的脚本文件
创建一个名字为init_tool.sh的脚本文件,内容如下:

#!/system/bin/sh

mkdir  -p /sdcard/logs/
logcat -b system -b events -b main -b radio -b kernel -n 10 -v threadtime -f /sdcard/logs/alllog -r5000

脚本名称及内容可自定义。
2、将脚本文件编译至系统,并放置在系统的system/bin目录下。
在device/rockchip/rk3326/device-common.mk文件中添加

# add for log inittool.sh
PRODUCT_COPY_FILES += \
       $(TARGET_DEVICE_DIR)/othertool/init_tool.sh:system/bin/init_tool.sh



其中$(TARGET_DEVICE_DIR)/othertool/init_tool.sh 这个是脚本在SDK的路径,可自定义。system/bin/init_tool.sh这个是脚本拷贝至系统的路径,需固定为system/bin。
完成后编译代码,可在out/target/product/rk3326_evb/system/bin/查看是否修改成功,如没有init_tool.sh 则需检查文件修改是否正确。

3、添加系统启动服务
在device/rockchip/common/init.rockchip.rc中添加

service inittool /system/bin/sh /system/bin/init_tool.sh
    #class core
    user root
    group root
    disabled
    oneshot
    seclabel u:r:inittool:s0



init.rc 的语法可自行网上查阅,因设计较多,这儿就不一一讲解,需要用到的时候再去查相关用法。

4、设置服务开机自启动
在device/rockchip/common/init.rk30board.rc中添加

@@ -249,7 +249,12 @@ on property:sys.boot_completed=1
     setprop persist.sys.first_booting false
     # if data is encrypt, when start gps too early, gpsd crashed
     start gpsd
-
+#Add INIT_TOOL SERVICE
+    wait /sdcard/Android
+    start inittool

注意,这儿启动服务一定要有on property:sys.boot_completed=1即等待系统启动完成后,再启动inittool服务。


5、给服务添加系统权限
在device/rockchip/common/sepolicy/file_contexts中添加

#for log tool
/system/bin/init_tool.sh                       u:object_r:inittool_exec:s0



device/rockchip/common/sepolicy/init.te中添加

#init tool
allow init inittool_exec:file  { getattr };



创建inittool.te放置在device/rockchip/common/sepolicy文件夹下
内容为

type inittool, domain;
type inittool_exec, exec_type, file_type;
#init_daemon_domain(rftoolcopy)

allow inittool shell_exec:file { getattr open read };
#allow rftoolcopy toolbox_exec:file { getattr execute read open execute_no_trans };
allow inittool storage_file:lnk_file { read   };
allow inittool tmpfs:dir { search  };
allow inittool mnt_user_file:dir { search  };
allow inittool mnt_user_file:lnk_file {  read };


注意:

inittool.te 这个文件名称需与上面创建的服务名称一致(上面创建的服务名称为inittool,即我们的权限文件为inittool.te),包括文件内容的名字,都需要以第三点创建的服务名称为标准,不可自定义命名

至此,我们的开机服务已经添加完成,重新编译系统,刷机后可验证服务已一直启动在后台,如需杀掉该服务,可使用kill 命令清理掉后台服务。但每次重启后都会再次启动。如不需要使用了,需在代码里设置关闭。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 9中,应用程序不能自动启动,除非用户明确地启用了自动启动权限。这是为了保护用户的隐私和设备的性能。 如果您需要在设备启动时自动运行脚本,可以考虑创建一个后台服务,并在启动时启动该服务。以下是一个简单的示例: 1. 创建一个后台服务类: ``` public class MyService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { // 在此处编写需要自动运行的脚本 return START_STICKY; } @Override public IBinder onBind(Intent intent) { return null; } } ``` 2. 在AndroidManifest.xml文件中声明该服务: ``` <service android:name=".MyService" android:enabled="true" android:exported="false" /> ``` 3. 在应用程序的启动Activity或Application类中启动该服务: ``` Intent intent = new Intent(this, MyService.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(intent); } else { startService(intent); } ``` 请注意,为了在Android 9及更高版本中正常工作,您需要启动一个前台服务。这可以通过调用startForegroundService()代替startService()来实现。 在启动服务时,您还需要请求自动启动权限。这可以通过向用户显示一个对话框并要求他们手动启用此权限来实现。以下是一个简单的示例: ``` if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { Intent intent = new Intent(); String packageName = getPackageName(); PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); if (!pm.isIgnoringBatteryOptimizations(packageName)) { intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:" + packageName)); startActivity(intent); } } ``` 请注意,这只是一个示例,您需要根据您的应用程序需求进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值