Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

test: Reply
 
LinkBack Thread Tools
Old August 29th, 2012, 12:12 PM   #1 (permalink)
ROM Developer
Thread Author (OP)
 
jetx2x's Avatar
 
Join Date: Mar 2012
Location: Charlotte, NC
Gender: Male
Posts: 1,714
 
Device(s): ZTE Warp (cm10, Sugar Kang) Lg Optimus Elite (cm9) Lg Spectrum (cm10.1) Samsung Galaxy SIII (Spri
Carrier: Sprint

Thanks: 238
Thanked 795 Times in 431 Posts
Default New to Programming

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

Advertisements
jetx2x is offline  
Reply With Quote
sponsored links
Old August 30th, 2012, 01:10 AM   #2 (permalink)
Junior Member
 
Join Date: Aug 2012
Location: Athens, Greece
Posts: 42
 
Device(s):
Carrier: Not Provided

Thanks: 6
Thanked 7 Times in 7 Posts
Default

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"});
Kostas G is offline  
Reply With Quote
Old August 31st, 2012, 10:17 PM   #3 (permalink)
ROM Developer
Thread Author (OP)
 
jetx2x's Avatar
 
Join Date: Mar 2012
Location: Charlotte, NC
Gender: Male
Posts: 1,714
 
Device(s): ZTE Warp (cm10, Sugar Kang) Lg Optimus Elite (cm9) Lg Spectrum (cm10.1) Samsung Galaxy SIII (Spri
Carrier: Sprint

Thanks: 238
Thanked 795 Times in 431 Posts
Default

Quote:
Originally Posted by Kostas G View Post
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
jetx2x is offline  
Reply With Quote
Old September 1st, 2012, 10:11 AM   #4 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,935
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 482 Times in 294 Posts
Default

can you post the entire logcat output? You left out some important info.
jonbonazza is offline  
Reply With Quote
Old September 1st, 2012, 10:53 AM   #5 (permalink)
ROM Developer
Thread Author (OP)
 
jetx2x's Avatar
 
Join Date: Mar 2012
Location: Charlotte, NC
Gender: Male
Posts: 1,714
 
Device(s): ZTE Warp (cm10, Sugar Kang) Lg Optimus Elite (cm9) Lg Spectrum (cm10.1) Samsung Galaxy SIII (Spri
Carrier: Sprint

Thanks: 238
Thanked 795 Times in 431 Posts
Default

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
jetx2x is offline  
Reply With Quote
Old September 1st, 2012, 12:14 PM   #6 (permalink)
ROM Developer
Thread Author (OP)
 
jetx2x's Avatar
 
Join Date: Mar 2012
Location: Charlotte, NC
Gender: Male
Posts: 1,714
 
Device(s): ZTE Warp (cm10, Sugar Kang) Lg Optimus Elite (cm9) Lg Spectrum (cm10.1) Samsung Galaxy SIII (Spri
Carrier: Sprint

Thanks: 238
Thanked 795 Times in 431 Posts
Default

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
jetx2x is offline  
Reply With Quote
Old September 2nd, 2012, 01:07 PM   #7 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,935
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 482 Times in 294 Posts
Default

Quote:
Originally Posted by jetx2x View Post
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
That's likely your issue.
jonbonazza is offline  
Reply With Quote
Old September 2nd, 2012, 01:22 PM   #8 (permalink)
ROM Developer
Thread Author (OP)
 
jetx2x's Avatar
 
Join Date: Mar 2012
Location: Charlotte, NC
Gender: Male
Posts: 1,714
 
Device(s): ZTE Warp (cm10, Sugar Kang) Lg Optimus Elite (cm9) Lg Spectrum (cm10.1) Samsung Galaxy SIII (Spri
Carrier: Sprint

Thanks: 238
Thanked 795 Times in 431 Posts
Default

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
jetx2x is offline  
Reply With Quote
Old September 3rd, 2012, 11:37 AM   #9 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,935
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 482 Times in 294 Posts
Default

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 androidnclick if youa re using xml, which I assume you are since you don't have any mention of an OnClickListener anywhere).
jonbonazza is offline  
Reply With Quote
Old September 5th, 2012, 10:09 AM   #10 (permalink)
ROM Developer
Thread Author (OP)
 
jetx2x's Avatar
 
Join Date: Mar 2012
Location: Charlotte, NC
Gender: Male
Posts: 1,714
 
Device(s): ZTE Warp (cm10, Sugar Kang) Lg Optimus Elite (cm9) Lg Spectrum (cm10.1) Samsung Galaxy SIII (Spri
Carrier: Sprint

Thanks: 238
Thanked 795 Times in 431 Posts
Default

Quote:
Originally Posted by jonbonazza View Post
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 androidnclick 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
jetx2x is offline  
Reply With Quote
sponsored links
Old September 5th, 2012, 10:13 AM   #11 (permalink)
ROM Developer
Thread Author (OP)
 
jetx2x's Avatar
 
Join Date: Mar 2012
Location: Charlotte, NC
Gender: Male
Posts: 1,714
 
Device(s): ZTE Warp (cm10, Sugar Kang) Lg Optimus Elite (cm9) Lg Spectrum (cm10.1) Samsung Galaxy SIII (Spri
Carrier: Sprint

Thanks: 238
Thanked 795 Times in 431 Posts
Default

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
jetx2x is offline  
Reply With Quote
Old September 5th, 2012, 11:23 AM   #12 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,935
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 482 Times in 294 Posts
Default

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:
startService(new Intent(this, YourServiceSubclass.class));
jonbonazza is offline  
Reply With Quote
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 06:35 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.