码迷,mamicode.com
首页 > 其他好文 > 详细

实现硬件访问服务

时间:2014-06-11 06:42:03      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   class   blog   code   

一、

1、定义硬件访问服务接口(为了进程间通信)

      ~/android-2.3_r1/frameworks/base

       ----Android.mk

       ----/core/java/android/os/IFregService.aidl

 

       Android系统提供了一种描述语言来定义具有跨进程访问能力的服务接口,这种描述语言称为Android接口描述语言(AIDL)。

       IFregService.aidl

package android.os;

interface IFregService {
	void setVal(int val);
	int getVal();
}
       Android.mk

LOCAL_SRC_FILES += /

   ....................................................................

   core/java/android/os/IVibratorService.aidl /

   core/java/android/os/IFregService.aidl /

   core/java/android/service/urlrenderer/IUrlRendererService.aidl /


2、编译

      编译aidl:

bubuko.com,布布扣

      生成的IFregService.java文件位于out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/os中。

      IFregService.java

/*
 * This file is auto-generated.  DO NOT MODIFY.
 * Original file: frameworks/base/core/java/android/os/IFregService.aidl
 */
package android.os;

public interface IFregService extends android.os.IInterface {
	/** Local-side IPC implementation stub class. */
	public static abstract class Stub extends android.os.Binder implements
			android.os.IFregService {
		private static final java.lang.String DESCRIPTOR = "android.os.IFregService";

		/** Construct the stub at attach it to the interface. */
		public Stub() {
			this.attachInterface(this, DESCRIPTOR);
		}

		/**
		 * Cast an IBinder object into an android.os.IFregService interface,
		 * generating a proxy if needed.
		 */
		public static android.os.IFregService asInterface(android.os.IBinder obj) {
			if ((obj == null)) {
				return null;
			}
			android.os.IInterface iin = (android.os.IInterface) obj
					.queryLocalInterface(DESCRIPTOR);
			if (((iin != null) && (iin instanceof android.os.IFregService))) {
				return ((android.os.IFregService) iin);
			}
			return new android.os.IFregService.Stub.Proxy(obj);
		}

		public android.os.IBinder asBinder() {
			return this;
		}

		@Override
		public boolean onTransact(int code, android.os.Parcel data,
				android.os.Parcel reply, int flags)
				throws android.os.RemoteException {
			switch (code) {
			case INTERFACE_TRANSACTION: {
				reply.writeString(DESCRIPTOR);
				return true;
			}
			case TRANSACTION_setVal: {
				data.enforceInterface(DESCRIPTOR);
				int _arg0;
				_arg0 = data.readInt();
				this.setVal(_arg0);
				reply.writeNoException();
				return true;
			}
			case TRANSACTION_getVal: {
				data.enforceInterface(DESCRIPTOR);
				int _result = this.getVal();
				reply.writeNoException();
				reply.writeInt(_result);
				return true;
			}
			}
			return super.onTransact(code, data, reply, flags);
		}

		private static class Proxy implements android.os.IFregService {
			private android.os.IBinder mRemote;

			Proxy(android.os.IBinder remote) {
				mRemote = remote;
			}

			public android.os.IBinder asBinder() {
				return mRemote;
			}

			public java.lang.String getInterfaceDescriptor() {
				return DESCRIPTOR;
			}

			public void setVal(int val) throws android.os.RemoteException {
				android.os.Parcel _data = android.os.Parcel.obtain();
				android.os.Parcel _reply = android.os.Parcel.obtain();
				try {
					_data.writeInterfaceToken(DESCRIPTOR);
					_data.writeInt(val);
					mRemote.transact(Stub.TRANSACTION_setVal, _data, _reply, 0);
					_reply.readException();
				} finally {
					_reply.recycle();
					_data.recycle();
				}
			}

			public int getVal() throws android.os.RemoteException {
				android.os.Parcel _data = android.os.Parcel.obtain();
				android.os.Parcel _reply = android.os.Parcel.obtain();
				int _result;
				try {
					_data.writeInterfaceToken(DESCRIPTOR);
					mRemote.transact(Stub.TRANSACTION_getVal, _data, _reply, 0);
					_reply.readException();
					_result = _reply.readInt();
				} finally {
					_reply.recycle();
					_data.recycle();
				}
				return _result;
			}
		}

		static final int TRANSACTION_setVal = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
		static final int TRANSACTION_getVal = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
	}

	public void setVal(int val) throws android.os.RemoteException;

	public int getVal() throws android.os.RemoteException;
}


      最后生成的framework.jar文件位于out/target/product/generic/system/framework中。


3、实现硬件访问服务

      ~/android-2.3_r1/frameworks/base/services/java/com/android/server

      ----FregService.java

package com.android.server;

import android.content.Context;
import android.os.IFregService;
import android.util.Slog;

public class FregService extends IFregService.Stub {
	private static final String TAG = "FregService";
	
	private int mPtr = 0;

	FregService() {
		mPtr = init_native();
		
		if(mPtr == 0) {
			Slog.e(TAG, "Failed to initialize freg service.");
		}
	}

	public void setVal(int val) {
		if(mPtr == 0) {
			Slog.e(TAG, "Freg service is not initialized.");
			return;
		}

		setVal_native(mPtr, val);
	}	

	public int getVal() {
		if(mPtr == 0) {
			Slog.e(TAG, "Freg service is not initialized.");
			return 0;
		}

		return getVal_native(mPtr);
	}
	
	private static native int init_native();//调用jni方法
    	private static native void setVal_native(int ptr, int val);
	private static native int getVal_native(int ptr);
};


4、编译

bubuko.com,布布扣

      编译生成的services.jar位于out/target/product/generic/system/framework中。


二、

1、启动硬件访问服务

      ~/android-2.3_r1/frameworks/base/services/java/com/android/server

      ----SystemServer.java

      修改ServerThread类的成员函数run的实现,如下所示:

@Override

     public void run() {

     ....................................................................................

            try {

                  Slog.i(TAG, "DiskStats Service");

                  ServiceManager.addService("diskstats", new DiskStatsService(context));

            } catch (Throwable e) {

                  Slog.e(TAG, "Failure starting DiskStats Service", e);

            }

            try {

                  Slog.i(TAG, "Hello Service");

                  ServiceManager.addService("hello", new FregService());

            } catch (Throwable e) {

                  Slog.e(TAG, "Failure starting Freg Service", e);

            }

2、编译
bubuko.com,布布扣

      编译生成的services.jar位于out/target/product/generic/system/framework中。

bubuko.com,布布扣

      最后把services.jar和framework.jar重新打包进入system.img,位于out/target/product/gerneric中。

实现硬件访问服务,布布扣,bubuko.com

实现硬件访问服务

标签:des   android   style   class   blog   code   

原文地址:http://blog.csdn.net/jltxgcy/article/details/29372453

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!