Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 開發Android應用程序來使用硬件訪問服務

開發Android應用程序來使用硬件訪問服務

編輯:關於Android編程

1、開發Android應用程序來使用硬件訪問服務

~/android-2.3_r1/packages/experimental/Freg

----AndroidManifest.java

----Android.mk

----src

----shy/luo/freg

----Freg.java

----res

----layout

----main.xml

----values

----string.xml

----drawable

----icon.png


Freg.java

package shy.luo.freg;

import android.app.Activity;
import android.os.ServiceManager;
import android.os.Bundle;
import android.os.IFregService;
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 Freg extends Activity implements OnClickListener {
	private final static String LOG_TAG = "shy.luo.freg.FregActivity";
	
	private IFregService fregService = null;

	private EditText valueText = null;
	private Button readButton = null;
	private Button writeButton = null;
	private Button clearButton = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

	fregService = IFregService.Stub.asInterface(
		ServiceManager.getService("freg"));
        
        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, "Freg Activity Created");
    }
    
    @Override
    public void onClick(View v) {
    	if(v.equals(readButton)) {
		try {
    			int val = fregService.getVal();
    			String text = String.valueOf(val);
    			valueText.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 = valueText.getText().toString();
    			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 = "";
    		valueText.setText(text);
    	}
    }
}


main.xml



    
    	
    	
    	
    	
    
     
    	
    	
    	
    

strings.xml



    Freg
    Value
    Please input a value...
    Read
    Write
    Clear

AndroidManifest.xml



    
        
            
                
                
            
        
    
 

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 := Freg

include $(BUILD_PACKAGE)

2、編譯

編譯成apk:

\

生成的Freg.apk位於out/target/product/generic/system/app中。<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8L3A+CjxwPiAgICAgILTysPyjujwvcD4KPHA+PGltZyBzcmM9"/uploadfile/Collfiles/20140609/201406090910363.png" alt="\">

最後把Freg.apk重新打包進入system.img,位於out/target/product/gerneric中。


運行:

\


結果如下:


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved