• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps New to Programming

jetx2x

Android Expert
Mar 24, 2012
1,714
790
Charlotte, NC
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:
package com.teamglitch.backup;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class OnAndroidBackupAppActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    /** Called when the user clicks the button */
    public void startBackup(View view) {
        // Do something in response to button
    	 Intent intent = new Intent(this, beginbackup.class);
    	 startActivity(intent);
    	}
    }


Code:
 /**
 * 
 */
package com.teamglitch.backup;

import java.io.IOException;

/**
 * @author Josh
 *
 */
public class beginbackup {
	void backup(String command)
	{     
		java.lang.Process p;  
		 
		   try {
			// Perform su to get root privileges  
			   p = Runtime.getRuntime().exec("su");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		try{    
	        Runtime.getRuntime().exec("/system/bin/sh /system/bin/onandroid.sh");
	    }
		catch (IOException e) 
	{
	    // TODO Auto-generated catch block
	    e.printStackTrace();}
	}
}

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
 
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:
Runtime.getRuntime().exec(new String[] { "su", 
       "/system/bin/sh", "/system/bin/onandroid.sh"});
 
Upvote 0
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:
Runtime.getRuntime().exec(new String[] { "su", 
       "/system/bin/sh", "/system/bin/onandroid.sh"});

Thx... I ran a logcat a few days ago and the only error I get is this
Code:
 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
 
Upvote 0
Haha... not all that experienced yet... I used a different keyword for the filter and found this

Code:
 E/AndroidRuntime( 1473): FATAL EXCEPTION: main
E/AndroidRuntime( 1473): java.lang.IllegalStateException: Could not execute method of the activity
E/AndroidRuntime( 1473): at android.view.View$1.onClick(View.java:2144)
E/AndroidRuntime( 1473): at android.view.View.performClick(View.java:2485)
E/AndroidRuntime( 1473): at android.view.View$PerformClick.run(View.java:9080)
E/AndroidRuntime( 1473): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 1473): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 1473): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 1473): at android.app.ActivityThread.main(ActivityThread.java:3717)
E/AndroidRuntime( 1473): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1473): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 1473): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
E/AndroidRuntime( 1473): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
E/AndroidRuntime( 1473): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1473): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime( 1473): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1473): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 1473): at android.view.View$1.onClick(View.java:2139)
E/AndroidRuntime( 1473): ... 11 more
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?
E/AndroidRuntime( 1473): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
E/AndroidRuntime( 1473): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
E/AndroidRuntime( 1473): at android.app.Activity.startActivityForResult(Activity.java:2827)
E/AndroidRuntime( 1473): at android.app.Activity.startActivity(Activity.java:2933)
E/AndroidRuntime( 1473): at com.teamglitch.backup.OnAndroidBackupAppActivity.startBackup(OnAndroidBackupAppActivity.java:19)
E/AndroidRuntime( 1473): ... 14 more

Sent from my N860 using Tapatalk 2 Beta-5
 
Upvote 0
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
 
Upvote 0
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).
 
Upvote 0
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).

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
 
Upvote 0
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
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones