I'm trying to make Android app that stops battery charging.
My Android device runs Android 12, and the value of compileSdk in build.gradle of this app is 32.
Ofcource, my Android device is rooted.
I followed this question and disabled SELinux by runnning "setenforce 0" command.
I found that I can stop battery charging by changing value of "/sys/class/power_supply/battery/input_suspend" from 0 to 1.
I have confirmed by running the following command on the PC to which a Android device is connected that I can really stop battery charging by doing this:
adb shell su
echo 1 > /sys/class/power_supply/battery/input_suspend
This worked well. So I started creating app that uses this method.
The MainActivity.java of this App:
package com.example.chargedisabler;
import static android.content.ContentValues.TAG;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Runtime.getRuntime().exec("su -c \"echo 1 > /sys/class/power_supply/battery/input_suspend\"");
} catch (IOException e) {
e.printStackTrace();
}
}
}
This not worked. Even if Root privileges were granted, charging was not stopped.
I changed the method and changed the MainActivity.java as follows:
package com.example.chargedisabler;
import static android.content.ContentValues.TAG;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
import java.io.OutputStream;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Process p = Runtime.getRuntime().exec("su");
OutputStream OStrm = p.getOutputStream();
OStrm.write(("echo 1 > /sys/class/power_supply/battery/input_suspend\n").getBytes("UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
This also not worked.
Next, on the PC to which the Android device is connected, I tried to stop battery charging in a different way than before:
adb shell su -c "echo 1 > /sys/class/power_supply/battery/input_suspend"
Oddly enough, this command outputs the following:
/system/bin/sh: can't create /sys/class/power_supply/battery/input_suspend: Permission denied
When I tried to read the contents in a similar way, somehow the file could be read successfully:
adb shell su -c "cat /sys/class/power_supply/battery/input_suspend"
This command outputs "0".
It seems that unless the command is executed in some correct way, the file will be kept secret for writing.
How can I change the value of "/sys/class/power_supply/battery/input_suspend" from within the application?
Or is there a better way to stop charging that works with any device?