Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 3.71 KB  |  hits: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Run a service with Root privileges or adding permissions with root
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import android.util.Log;
  7.  
  8. public abstract class RootAccess {
  9.     private static final String TAG = "RootAccess";
  10.     protected abstract ArrayList<String> runCommandsWithRootAccess();
  11.  
  12.     //Check for Root Access
  13.     public static boolean hasRootAccess() {
  14.         boolean rootBoolean = false;
  15.         Process suProcess;
  16.  
  17.         try {
  18.             suProcess = Runtime.getRuntime().exec("su");
  19.  
  20.             DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
  21.             DataInputStream is = new DataInputStream(suProcess.getInputStream());
  22.  
  23.             if (os != null && is != null) {
  24.                 // Getting current user's UID to check for Root Access
  25.                 os.writeBytes("idn");
  26.                 os.flush();
  27.  
  28.                 String outputSTR = is.readLine();
  29.                 boolean exitSu = false;
  30.                 if (outputSTR == null) {
  31.                     rootBoolean = false;
  32.                     exitSu = false;
  33.                     Log.d(TAG, "Can't get Root Access or Root Access deneid by user");
  34.                 } else if (outputSTR.contains("uid=0")) {
  35.                     //If is contains uid=0, It means Root Access is granted
  36.                     rootBoolean = true;
  37.                     exitSu = true;
  38.                     Log.d(TAG, "Root Access Granted");
  39.                 } else {
  40.                     rootBoolean = false;
  41.                     exitSu = true;
  42.                     Log.d(TAG, "Root Access Rejected: " + is.readLine());
  43.                 }
  44.  
  45.                 if (exitSu) {
  46.                     os.writeBytes("exitn");
  47.                     os.flush();
  48.                 }
  49.             }
  50.         } catch (Exception e) {
  51.             rootBoolean = false;
  52.             Log.d(TAG, "Root access rejected [" + e.getClass().getName() + "] : " + e.getMessage());
  53.         }
  54.  
  55.         return rootBoolean;
  56.     }
  57.  
  58.     //Execute commands with ROOT Permission
  59.     public final boolean execute() {
  60.         boolean rootBoolean = false;
  61.  
  62.         try {
  63.             ArrayList<String> commands = runCommandsWithRootAccess();
  64.             if ( commands != null && commands.size() > 0) {
  65.                 Process suProcess = Runtime.getRuntime().exec("su");
  66.  
  67.                 DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
  68.  
  69.                 // Execute commands with ROOT Permission
  70.                 for (String currentCommand : commands) {
  71.                     os.writeBytes(currentCommand + "n");
  72.                     os.flush();
  73.                 }
  74.  
  75.                 os.writeBytes("exitn");
  76.                 os.flush();
  77.  
  78.                 try {
  79.                     int suProcessRetval = suProcess.waitFor();
  80.                     if ( suProcessRetval != 255) {
  81.                         // Root Access granted
  82.                         rootBoolean = true;
  83.                     } else {
  84.                         // Root Access denied
  85.                         rootBoolean = false;
  86.                     }
  87.                 } catch (Exception ex) {
  88.                     Log.e(TAG, "Error executing Root Action", ex);
  89.  
  90.                 }
  91.             }
  92.         } catch (IOException ex) {
  93.             Log.w(TAG, "Can't get Root Access", ex);
  94.         } catch (SecurityException ex) {
  95.             Log.w(TAG, "Can't get Root Access", ex);
  96.         } catch (Exception ex) {
  97.             Log.w(TAG, "Error executing operation", ex);
  98.         }
  99.  
  100.         return rootBoolean;
  101.     }
  102.  
  103.  
  104. }
  105.        
  106. <receiver android:name=".SmsReceiver" >
  107.         <intent-filter android:priority="100" >
  108.             <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  109.         </intent-filter>
  110.     </receiver>