Android开机自动执行脚本

0.前言

由于作者本人初次接触Android,所以本文所述内容只是操作方法,没有深究原理。各位可自行深入学习研究。

1.需求描述

需要在Android开机启动之后,自动执行一个脚本,假设这个脚本是 test.sh。脚本中只执行1条打印:

#!/bin/bash

echo "auto start test.sh!"

2.需求分析

根据需求描述,可以推导有两件事情要做:

  1. 首先将 test.sh 脚本文件预置到Android中的某个路径下,假设放在system/app;
  2. 编译后,下载到开发板,上电自动执行 test.sh 脚本。

3.操作步骤

  1. 添加rc文件,如下:注意rc文件最后一定要有空行,否则编译报错!
service test /system/app/test.sh
    class main
    user root
    group root

on property:sys.boot_completed=1
    start test

  1. 预置文件,参考Android系统预置文件。将 test.sh 脚本拷贝到相应的路径上。比如,我将其放在 /system/app 下;将rc文件预置到 out/target/product/xxx/system/etc/init 下(xxx是你的项目名称)。 Android.mk 如下:
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

$(shell cp -af $(LOCAL_PATH)/test.sh $(TARGET_OUT)/app)
$(shell cp -af $(LOCAL_PATH)/test.rc $(TARGET_OUT)/etc/init)
  1. 还可能需要给test.sh赋予执行权限。我的是在fs_config.cpp中修改权限的。
static const struct fs_path_config android_files[] = {
    //省略其他不相关权限配置
    { 00755, AID_ROOT,      AID_SHELL,     0, "system/app/*"},//给/system/app下的文件赋予755权限
    //省略其他不相关权限配置
};
  1. 整体编译后,烧写到开发板,可自动执行。

4.待完善

按照第3节步骤操作完成后,通过 adb shell 指令,查看日志,如下:

test:/ $ cd system/app
test:/system/app $ ls -l
-rwxr-xr-x 1 root shell   26 2022-02-09 20:13 test.sh
test:/system/app $ logcat |grep test.sh
01-01 08:00:16.657     0     0 E init    : Could not start service 'test' as part of class 'main': File /system/app/test.sh(labeled "u:object_r:system_file:s0") has incorrect label or no domain transition from u:r:init:s0 to another SELinux domain defined. Have you configured your service correctly? https://source.android.com/security/selinux/device-policy#label_new_services_and_address_denials
02-10 11:10:22.710     0     0 I init    : Command 'start test' action=sys.boot_completed=1 (/system/etc/init/test.rc:7) took 0ms and failed: Could not start service: File /system/app/test.sh(labeled "u:object_r:system_file:s0") has incorrect label or no domain transition from u:r:init:s0 to another SELinux domain defined. Have you configured your service correctly? https://source.android.com/security/selinux/device-policy#label_new_services_and_address_denials

可以看到确实自动执行了 test.rc 文件,只不过在执行 /system/app/test.sh 时出错了,原因是SELinux的问题。这部分我还没有深入研究,待后期完善。只要解决了SELinux的权限问题,就可以正常启动 test.sh 了。

谢绝转载!

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
发出的红包

打赏作者

fengwang0301

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值