packages/experimental目录中添加Freg目录,在Freg目录下添加App工程文件,工程目录结构如下
Freg/
├── AndroidManifest.xml
├── Android.mk
├── README
├── res
│ ├── drawable
│ │ └── ic_launcher.png
│ ├── layout
│ │ └── main.xml
│ └── values
│ └── strings.xml
└── src
└── com
└── example
└── Freg.java
AndroidManifest.xml文件中内容
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.freg"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="23"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light" >
<activity android:name=".Freg">
<intent-filter>
<!-- The MAIN action describes a main entry point into an
activity, without any associated data. -->
<action android:name="android.intent.action.MAIN" />
<!-- This places this activity into the main app list. -->
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Android.mk文件中内容
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Only compile source java files in this apk.
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_PACKAGE_NAME := Freg
#LOCAL_SDK_VERSION := current
LOCAL_MODULE_TAGS := optional
include $(BUILD_PACKAGE)
main.xml文件内容为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_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>
strings.xml文件中内容
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Simple strings. -->
<string name="app_name">Freg</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>
Freg.java文件中源代码
package com.example.freg;
import android.app.Activity;
import android.os.ServiceManager;
import android.os.Bundle;
import android.os.IFregService;
import android.os.RemoteException;
import android.os.IFregService;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.text.TextUtils;
import android.widget.Toast;
public class Freg extends Activity implements OnClickListener {
private final static String LOG_TAG = "com.example.freg.FregActivity";
private IFregService fregService = null;
private EditText valText = null;
private Button readButton = null;
private Button writeButton = null;
private Button clearButton = null;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fregService = IFregService.Stub.asInterface(
ServiceManager.getService("freg"));
valText = (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, "Freg Activity Created.");
}
@Override
public void onClick(View v){
if (v.equals(readButton)){
try{
int val = fregService.getVal();
String text = String.valueOf(val);
valText.setText(text);
} catch (RemoteException e) {
Log.e(LOG_TAG, "Remote Exception while reading value from freg service.");
}
} else if (v.equals(writeButton)){
try {
String text = valText.getText().toString();
if (TextUtils.isEmpty(valText.getText()))
{
Toast.makeText(getApplicationContext(), "输入为空", Toast.LENGTH_SHORT).show();
}
else
{
int val = Integer.parseInt(text);
fregService.setVal(val);
}
} catch (RemoteException e) {
Log.e(LOG_TAG, "Remote Exception while writing value to freg service.");
}
} else if (v.equals(clearButton)){
String text = "";
valText.setText(text);
}
}
}
按如下命令编译
$ mmm ./packages/experimental/Freg/
并打包,打包命令如下:
$ make snod
运行emulator
$ emulator -kernel kernel/goldfish/arch/arm/boot/zImage &
参考
《Android系统源代码情景分析》第2.5节