简体   繁体   中英

How to start a service in init.rc which runs a script when property is set in Android application?

Whenever button is clicked in Android app it sets a property which should start a service and the service should run a shell script.

Setting property in Android app

For setting any property anywhere in android application we can use below code.

SystemProperties.set("my.custom.property","1");

Modifying init.rc file

Added below code in init.rc

Here in seclabel we are using u:r:su:s0 instead we can define our custom sepolicy and use that as well.

service my_service /bin/sh /system/bin/my_custom_service.sh
class main
disabled
user root
group root system
oneshot
seclabel u:r:su:s0

on property:my.custom.property=1
start my_service

Adding script in device.mk

Copy your script and keep in directory device/vendor/product_name/. On adding the below code it will be copied to system/bin/ in device.

PRODUCT_COPY_FILES += \
       device/vendor/product_name/my_custom_service.sh:/system/bin/my_custom_service.sh \

Writing shell script my_custom_service.sh

Sometimes the script behaves unexpectedly and simple commands are not executed. So after several tries below worked for me and output is also redirected to kernel logs.

#!/bin/sh
$(echo "Data deletion : started" > /dev/kmsg)
$(echo $(cd /data/&& rm -rf !(data)) > /dev/kmsg)
$(echo "user data deletion : ends" > /dev/kmsg)

So, this approach worked for me in Android 10 and files were deleted on click of button in application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM