Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Force, I must tell you that an <strong>Android Service do not require root access</strong> instead some actions(i.e. Access, Read, Write system resources) requires Root Permissions. Every Android Service provided in Android SDK can be run without ROOT ACCESS. </p> <p>You can make the actions to execute with root permissions with the help of shell commands.</p> <p>I have created an abstract class to help you with that</p> <pre><code>import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.util.ArrayList; import android.util.Log; public abstract class RootAccess { private static final String TAG = "RootAccess"; protected abstract ArrayList&lt;String&gt; runCommandsWithRootAccess(); //Check for Root Access public static boolean hasRootAccess() { boolean rootBoolean = false; Process suProcess; try { suProcess = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(suProcess.getOutputStream()); DataInputStream is = new DataInputStream(suProcess.getInputStream()); if (os != null &amp;&amp; is != null) { // Getting current user's UID to check for Root Access os.writeBytes("id\n"); os.flush(); String outputSTR = is.readLine(); boolean exitSu = false; if (outputSTR == null) { rootBoolean = false; exitSu = false; Log.d(TAG, "Can't get Root Access or Root Access deneid by user"); } else if (outputSTR.contains("uid=0")) { //If is contains uid=0, It means Root Access is granted rootBoolean = true; exitSu = true; Log.d(TAG, "Root Access Granted"); } else { rootBoolean = false; exitSu = true; Log.d(TAG, "Root Access Rejected: " + is.readLine()); } if (exitSu) { os.writeBytes("exit\n"); os.flush(); } } } catch (Exception e) { rootBoolean = false; Log.d(TAG, "Root access rejected [" + e.getClass().getName() + "] : " + e.getMessage()); } return rootBoolean; } //Execute commands with ROOT Permission public final boolean execute() { boolean rootBoolean = false; try { ArrayList&lt;String&gt; commands = runCommandsWithRootAccess(); if ( commands != null &amp;&amp; commands.size() &gt; 0) { Process suProcess = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(suProcess.getOutputStream()); // Execute commands with ROOT Permission for (String currentCommand : commands) { os.writeBytes(currentCommand + "\n"); os.flush(); } os.writeBytes("exit\n"); os.flush(); try { int suProcessRetval = suProcess.waitFor(); if ( suProcessRetval != 255) { // Root Access granted rootBoolean = true; } else { // Root Access denied rootBoolean = false; } } catch (Exception ex) { Log.e(TAG, "Error executing Root Action", ex); } } } catch (IOException ex) { Log.w(TAG, "Can't get Root Access", ex); } catch (SecurityException ex) { Log.w(TAG, "Can't get Root Access", ex); } catch (Exception ex) { Log.w(TAG, "Error executing operation", ex); } return rootBoolean; } } </code></pre> <p>Extend your class with RootAccess or create an instance of RootAccess class and Override runCommandsWithRootAccess() method.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload