1. Download the #1 Android News App:   EarlyBird - News for Android
    Dismiss Notice

New to Programming

Last Updated:

  1. jetx2x

    jetx2x Well-Known Member Developer

    Hi! Ive been into rom development for some time but up until now I've only been a modder/themer... I'm trying to create a simple app that can request root privileges and run a sh script for my rom... but im kind of confused... I just need the app to respond to a button click by running the script... here is my code

    Code (Text):
    1. package com.teamglitch.backup;
    3. import android.app.Activity;
    4. import android.content.Intent;
    5. import android.os.Bundle;
    6. import android.view.View;
    8. public class OnAndroidBackupAppActivity extends Activity {
    9.     /** Called when the activity is first created. */
    10.     @Override
    11.     public void onCreate(Bundle savedInstanceState) {
    12.         super.onCreate(savedInstanceState);
    13.         setContentView(R.layout.main);
    14.     }
    15.     /** Called when the user clicks the button */
    16.     public void startBackup(View view) {
    17.         // Do something in response to button
    18.          Intent intent = new Intent(this, beginbackup.class);
    19.          startActivity(intent);
    20.         }
    21.     }

    Code (Text):
    1.  /**
    2.  *
    3.  */
    4. package com.teamglitch.backup;
    6. import java.io.IOException;
    8. /**
    9.  * @author Josh
    10.  *
    11.  */
    12. public class beginbackup {
    13.     void backup(String command)
    14.     {    
    15.         java.lang.Process p;  
    17.            try {
    18.             // Perform su to get root privileges  
    19.                p = Runtime.getRuntime().exec("su");
    20.         } catch (IOException e) {
    21.             // TODO Auto-generated catch block
    22.             e.printStackTrace();
    23.         }  
    24.         try{    
    25.             Runtime.getRuntime().exec("/system/bin/sh /system/bin/onandroid.sh");
    26.         }
    27.         catch (IOException e)
    28.     {
    29.         // TODO Auto-generated catch block
    30.         e.printStackTrace();}
    31.     }
    32. }
    When I click the button to begin the backup I get force closes.... I don't really have any coding experience so any help would be appreciated

  2. Kostas G

    Kostas G Active Member

    When you get a Force Close, an error message is printed in the Log Cat. If you are using Eclipse for development a Log Cat View is provided, where you can see this message. If you are running it in a device, you can install one of the many applications in the Market for viewing the messages.

    Seeing the messages is very important both for your debugging and for when asking for help. You should read them and if you can't still understand what is happening, copy them to your question.

    Without seeing the error message I can only guess what is happening:

    - Your code requires a rooted device. I don't know if you can run it in the emulator
    - You should have the 'su' command and the script in the same process. The way you have it, they are in two different processes.

    Try something like this:

    Code (Text):
    2. Runtime.getRuntime().exec(new String[] { "su",
    3.        "/system/bin/sh", "/system/bin/onandroid.sh"});
  3. jetx2x

    jetx2x Well-Known Member Developer

    Thx... I ran a logcat a few days ago and the only error I get is this
    Code (Text):
    1.  E/AndroidRuntime( 7427): at com.teamglitch.backup.OnAndroidBackupAppActivity.startBackup(OnAndroidBackupAppActivity.java:19)
    And I will try out your suggestion and see what happens from there

    Sent from my N860 using Tapatalk 2 Beta-5
  4. jonbonazza

    jonbonazza Well-Known Member

    can you post the entire logcat output? You left out some important info.
  5. jetx2x

    jetx2x Well-Known Member Developer

    Haha... not all that experienced yet... I used a different keyword for the filter and found this

    Code (Text):
    2.  E/AndroidRuntime( 1473): FATAL EXCEPTION: main
    3. E/AndroidRuntime( 1473): java.lang.IllegalStateException: Could not execute method of the activity
    4. E/AndroidRuntime( 1473): at android.view.View$1.onClick(View.java:2144)
    5. E/AndroidRuntime( 1473): at android.view.View.performClick(View.java:2485)
    6. E/AndroidRuntime( 1473): at android.view.View$PerformClick.run(View.java:9080)
    7. E/AndroidRuntime( 1473): at android.os.Handler.handleCallback(Handler.java:587)
    8. E/AndroidRuntime( 1473): at android.os.Handler.dispatchMessage(Handler.java:92)
    9. E/AndroidRuntime( 1473): at android.os.Looper.loop(Looper.java:130)
    10. E/AndroidRuntime( 1473): at android.app.ActivityThread.main(ActivityThread.java:3717)
    11. E/AndroidRuntime( 1473): at java.lang.reflect.Method.invokeNative(Native Method)
    12. E/AndroidRuntime( 1473): at java.lang.reflect.Method.invoke(Method.java:507)
    13. E/AndroidRuntime( 1473): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
    14. E/AndroidRuntime( 1473): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
    15. E/AndroidRuntime( 1473): at dalvik.system.NativeStart.main(Native Method)
    16. E/AndroidRuntime( 1473): Caused by: java.lang.reflect.InvocationTargetException
    17. E/AndroidRuntime( 1473): at java.lang.reflect.Method.invokeNative(Native Method)
    18. E/AndroidRuntime( 1473): at java.lang.reflect.Method.invoke(Method.java:507)
    19. E/AndroidRuntime( 1473): at android.view.View$1.onClick(View.java:2139)
    20. E/AndroidRuntime( 1473): ... 11 more
    21. E/AndroidRuntime( 1473): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.teamglitch.backup/com.teamglitch.backup.beginbackup}; have you declared this activity in your AndroidManifest.xml?
    22. E/AndroidRuntime( 1473): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
    23. E/AndroidRuntime( 1473): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
    24. E/AndroidRuntime( 1473): at android.app.Activity.startActivityForResult(Activity.java:2827)
    25. E/AndroidRuntime( 1473): at android.app.Activity.startActivity(Activity.java:2933)
    26. E/AndroidRuntime( 1473): at com.teamglitch.backup.OnAndroidBackupAppActivity.startBackup(OnAndroidBackupAppActivity.java:19)
    27. E/AndroidRuntime( 1473): ... 14 more
    Sent from my N860 using Tapatalk 2 Beta-5
  6. jetx2x

    jetx2x Well-Known Member Developer

    Call me an idiot but I forgot to add my activity to the manifest... ill add it to the manifest and then see what happens

    Sent from my N860 using Tapatalk 2 Beta-5
  7. jonbonazza

    jonbonazza Well-Known Member

    That's likely your issue.
  8. jetx2x

    jetx2x Well-Known Member Developer

    OK... added the activity to the manifest and it stopped fcing... but now it just opens up a blank screen after I press the button... doesn't request root... doesn't execute anything... what have I done wrong? All I need for it to do is request root and execute one specific script... I plan on expanding its functionality later on but for now I want to keep it simple...

    Sent from my N860 using Tapatalk 2 Beta-5
  9. jonbonazza

    jonbonazza Well-Known Member

    Your beginbackup class isn't an activity. It needs to be an activity and the backup method needs to be called from onCreate().

    With that said, that design is very poor. Instead, the backup method should be a part of your main activity and should be called directly inside onClick(). (or android:eek:nclick if youa re using xml, which I assume you are since you don't have any mention of an OnClickListener anywhere).
  10. jetx2x

    jetx2x Well-Known Member Developer

    Ahhh... I see what you mean... I tried to place it in a different class file because it seemed simpler to me at the time... but your method makes it even more simple... thx for the advice

    Sent from my N860 using Tapatalk 2 Beta-5
  11. jetx2x

    jetx2x Well-Known Member Developer

    But one more question... if I wanted to make this a service rather than an activity... would it have to go into a different class file? The script that I want to run creates a Nandroid backup without having to turn the phone off... so I was thinking that maybe I should make it a service that runs in the background and then sends a toast or notification in the status bar confirming that it either completed or that there was a problem

    Sent from my N860 using Tapatalk 2 Beta-5
  12. jonbonazza

    jonbonazza Well-Known Member

    To make a Service, you need to make another class that is a subclass of Service, rather than Activity and then in your startBackup method, you would call
    Code (Text):
    2. startService(new Intent(this, YourServiceSubclass.class));

Share This Page