Reputation: 39
Whenever button is clicked in Android app it sets a property which should start a service and the service should run a shell script.
Upvotes: 0
Views: 2651
Reputation: 39
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.
Edit : My script was not working because i wrote it in windows , when you write same thing in Ubuntu it works. So better write scripts in ubuntu
#!/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.
Upvotes: 2