[Android Software/Hacking General [Developers Only]] llrraa2010:
Android test application on the service to hardware

1. Hello.java

package shy.luo.hello;

import shy.luo.hello.R;

import android.app.Activity;

import android.os.ServiceManager;

import android.os.Bundle;

import android.os.IHelloService;

import android.os.RemoteException;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

public class Hello extends Activity implements OnClickListener {

private final static String LOG_TAG = "shy.luo.renju.Hello";

private IHelloService helloService = null;

private EditText valueText = null;

private Button readButton = null;

private Button writeButton = null;

private Button clearButton = null;

/** Called when the activity is first created. */

[MENTION=439709]override[/MENTION]

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

helloService = IHelloService.Stub.asInterface(

ServiceManager.getService("hello"));

valueText = (EditText)findViewById(R.id.edit_value);

readButton = (Button)findViewById(R.id.button_read);

writeButton = (Button)findViewById(R.id.button_write);

clearButton = (Button)findViewById(R.id.button_clear);

readButton.setOnClickListener(this);

writeButton.setOnClickListener(this);

clearButton.setOnClickListener(this);

Log.i(LOG_TAG, "Hello Activity Created");

}

[MENTION=439709]override[/MENTION]

public void onClick(View v) {

if(v.equals(readButton)) {

try {

int val = helloService.getVal();

String text = String.valueOf(val);

valueText.setText(text);

} catch (RemoteException e) {

Log.e(LOG_TAG, "Remote Exception while reading value from device.");

}

}

else if(v.equals(writeButton)) {

try {

String text = valueText.getText().toString();

int val = Integer.parseInt(text);

helloService.setVal(val);

} catch (RemoteException e) {

Log.e(LOG_TAG, "Remote Exception while writing value to device.");

}

}

else if(v.equals(clearButton)) {

String text = "";

valueText.setText(text);

}

}

}

res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

android:gravity="center">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/value">

</TextView>

<EditText

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/edit_value"

android:hint="@string/hint">

</EditText>

</LinearLayout>

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:gravity="center">

<Button

android:id="@+id/button_read"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/read">

</Button>

<Button

android:id="@+id/button_write"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/write">

</Button>

<Button

android:id="@+id/button_clear"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/clear">

</Button>

</LinearLayout>

</LinearLayout>

res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="app_name">Hello</string>

<string name="value">Value</string>

<string name="hint">Please input a value...</string>

<string name="read">Read</string>

<string name="write">Write</string>

<string name="clear">Clear</string>

</resources>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="shy.luo.hello"

android:versionCode="1"

android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".Hello"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

2. Android.mk

copy dir Hello to packages/experimental, add Android.mk:

USER-NAME@MACHINE-NAME:~/Android/packages/experimental$ vi Android.mk

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES := $(call all-subdir-java-files)

LOCAL_PACKAGE_NAME := Hello

include $(BUILD_PACKAGE)

3. build hello and repack system.img

USER-NAME@MACHINE-NAME:~/Android$ mmm packages/experimental/Hello

USER-NAME@MACHINE-NAME:~/Android$ make snod