在A20上演示老罗的Android硬件抽象层(HAL)概要介绍和学习计划5--关于APP部分

来源:互联网 发布:尼格罗人种知乎 编辑:程序博客网 时间:2024/05/17 03:33

在/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