[Android Software Development] Meltus:
Android Java

(Android specific examples)

******************************

Contents

- Root Access

- Buttons

- Exit Button

- Intents (next screen)

- Writing files to SDcard

*******************************

- Root Access

If you're developing an application that requires root access this is something you will need to know.

There are a few variations and diferent ways of acheiving this, but in my opinion, this is the most stable and problem free way i've found.

Simply insert this bit of code after the 'setContentView'.

try {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
{
os.writeBytes("mount -o rw,remount -t yaffs2 /dev/block/mtdblock03 /system\n" +
"exit \n");
os.flush();
process.waitFor();
}

} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}

What does this do?

Well, not only does it attempt to gain root access, it will pause the application untill the user grants it SU access. Without the "process.waitFor();' line (and the second 'catch'), your application can get a little messy because the screen will be displayed underneath.

The 'os.writeBytes' statement is, as you probable guessed, holds the command you want to execute as SU. In the above example, it will remount the filesystem as Read/Write and then exit. Remember, whatever commands you use, you will need to put a '\n' to signify the end of a line.

Warning: NEVER leave this bit completely blank, as your application will lock up. This is because you're opening up a command line and not closing it.

Simply chaning it to "exit\n" would not issue any command, but would still ask for root access, so this could be quite useful.

- Buttons

Perhaps one of the most important parts of your application; buttons are very simple to implement but can be a little confusing at first.

First of all you will need to place one on your "main.xml", or whatever yours is called, or type in the code below (which i prefer, as dragging it across from the Views list messes up the indentation and makes it hard to edit).

You don't have to bother with code for the button layout if you don't want to, as it can all be done from the properties menu, but i wouldn't advise you do it this way.

Type this in your 'main.xml':

<Button
android:text="Button01"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>

As you probably guessed, "wrap_content" can be replaced by either "fill_parent" or a numerical value in either 'px' or 'dp'. You should also give your button a more relevent name than Button01. For ease, i will call it ExampleButton.

Now that's done, move across to your .Java file. For your button to work we will need to implement a 'click listener', but first you will need to add the following to the top of your java file (before the Public Void bit).

private Button ExampleButton;

and then, in the main body add:

ExampleButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {;
*/ code goes here /*
}
});

Then your button is ready for code! (see below!)

- Exit Button

This one stumped me for a while, but as it turns out, it's incredibly simple to do. First you will need to create a button (see above) to act as the Exit button.

Once you've done that, add the following before the public void (this code can be added directly to the button, but i prefer doing it like this so it can be called from anywhere instead of typing it all out again).

For example:

public class Example extends Activity {

[B]public void killActivity() {
this.finish();
}[/B]

public void ...etc

Once done, this will allow you to simply insert "KillActivity();" to close your application!

So here's my exit button:

Exit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {;
killActivity();
}
});

Easy!

- Intents (next screen)

When your app starts getting a bit more advanced you may want more than one screen. This is quite easy to acheieve but can be quite messy if not done right.

Ok, first you'll have to create another .xml file (just copy and paste main.xml and delete all it's contents, that's the easy way! We'll call it screen2.xml).

Now, in your main.xml add the following code:

Intent a = new Intent(main.this, screen2.class);
startActivity(a);

You'll notice it will kick out a few errors. That's because your new screen2.xml hasn't been given an activity yet.

Open up your AndroidManifest.xml and add the following under your main activity (After the </activity> tag, but before the </application> tag!):

<activity android:name=".screen2"
android:label="@string/app_name">
<intent-filter>
</intent-filter>
</activity>

This should open up another identical activity over the top of the original one. If you want to close your original one instead of having both open you can just add your 'KillActivity();' after the code above!

- Writing files to SDcard

This little tutorial will tell you how to store files in your program and then on a button press, move them to a directory on your SDcard.

First of all, this tut requires you to create an additional folder for your app, so locate where the project is stored and under the folder 'res' (where you have drawable, layout, etc.) create another folder called "raw".

Now, whatever files you wish to store will have to be renamed.

For the files to be accepted you must remove any capital letters from the file name, any symbols and you also must remove any extension. So, a text file called 'Text1.2.txt' would have to be renamed to 'text1'. You must remember the extention though, as this will have to be restored manually later.

once your file is in place, you can create a directory for your files to go in. Using the command we learned for the 'Gaining Root Access' tutorial, we will create an 'Example' folder on your sdcard:

try {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
{
os.writeBytes("mkdir sdcard/example\n" +
"exit \n");
os.flush();
process.waitFor();
}

} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}

Once that's done, you can add the code to move the file (usually to a button press):

try {
InputStream ins = getResources().openRawResource(R.raw.text1);
int size = ins.available();
// Read the entire resource into a local byte buffer.
byte[] buffer = new byte[size];
ins.read(buffer);
ins.close();

FileOutputStream fos = new FileOutputStream("/sdcard/Example/Text1.2.txt");
fos.write(buffer);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}

More to come! :D