在/packages/apps/下建立myapp,用eclipse建立個工程,這樣利用ADT產生設定檔快點,複製到/packages/apps/myapp/把gen/com/example/myapp/R.java定義的類刪掉,別重複了。
主要代碼如下:
/packages/apps/myapp/src/com/example/myapp/MainActivity.java:
package com.example.myapp;
import com.example.myapp.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 MainActivity extends Activity implements OnClickListener {
private final static String LOG_TAG = "com.example.myapp.Hello";
private IHelloService helloService = null;
private EditText valueText = null;
private Button readButton = null;
private Button writeButton = null;
private Button clearButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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");
}
@Override
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);
}
}
//@Override
//public boolean onCreateOptionsMenu(Menu menu) {
// // Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.main, menu);
// return true;
//}
}
然後是/packages/apps/myapp的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 := myapp
include $(BUILD_PACKAGE)
LOCAL_PACKAGE_NAME := myapp 的myapp要不寫在build裡的*.mk或者去
/device/softwinner/wing-XXX/wing_XXX.mk添加:
PRODUCT_PACKAGES += \
myapp
以上方法由大家喜歡了,只要達到目的就可以。最後make下就可以了:
主要編譯出來的或者關聯的東西有:
/system/lib/libsystem_server.so
/system/lib/hw/hello.default.so
/system/app/myapp.apk