android_添加服务给应用使用

增加fakeled服务玩(aosp-4.4)
1.mkdir -p frameworks/base/bigbird/java/android/fakeled/
2.vi frameworks/base/bigbird/java/android/fakeled/IFakeLed.aidl
// for led flash;
// by bigbird;
// on 2014-1-8;
package android.fakeled;

import android.os.IInterface;
import android.os.Binder;
import android.os.IBinder;

interface IFakeLed{
	int createTest();
	int setLedFlashEnable(int enable);
	int getLedFlashEnable();
}
3.vi frameworks/base/bigbird/java/android/fakeled/FakeLedManager.java
// for led flash;
// by bigbird;
// on 2014-1-8;
package android.fakeled;

import android.content.Context;
import android.os.RemoteException;

public class FakeLedManager
{
	Context mContext;
	IFakeLed mService;
	
	public FakeLedManager(Context context, IFakeLed service)
	{
		mContext = context;
		mService = service;
	}

	public int createTest()
	{
		try{
			return mService.createTest();
		} catch(RemoteException e){
			return -1;
		}		
	}
	public int setLedFlashEnable(int enable)
	{
		try{
			return mService.setLedFlashEnable(enable);
		} catch(RemoteException e){
			return -1;
		}		
	}
	public int getLedFlashEnable()
	{
		try{
			return mService.getLedFlashEnable();
		} catch(RemoteException e){
			return -1;
		}		
	}
}
4.vi build/core/pathmap.mk
diff --git a/build/core/pathmap.mk b/build/core/pathmap.mk
index 167d680..5dfc543 100644
--- a/build/core/pathmap.mk
+++ b/build/core/pathmap.mk
@@ -90,7 +90,6 @@ FRAMEWORKS_BASE_SUBDIRS := \
            telephony \
            wifi \
            keystore \
- bigbird \
         )
5.vi frameworks/base/Android.mk
diff --git a/frameworks/base/Android.mk b/frameworks/base/Android.mk
index 3b9e8cf..166db4e 100644
--- a/frameworks/base/Android.mk
+++ b/frameworks/base/Android.mk
@@ -267,7 +267,6 @@ LOCAL_SRC_FILES += \
        packages/services/PacProcessor/com/android/net/IProxyService.aidl \
        packages/services/Proxy/com/android/net/IProxyCallback.aidl \
        packages/services/Proxy/com/android/net/IProxyPortListener.aidl \
- bigbird/java/android/fakeled/IFakeLed.aidl \
 # FRAMEWORKS_BASE_JAVA_SRC_DIRS comes from build/core/pathmap.mk
 LOCAL_AIDL_INCLUDES += $(FRAMEWORKS_BASE_JAVA_SRC_DIRS)
6.make update-api             --->update api;
7.make             --->compile ok;
//
8.mkdir frameworks/base/services/java/com/android/server/bigbird
9.vi frameworks/base/services/java/com/android/server/bigbird/FakeLedService.java
// Service for led flash;
// by bigbird;
// on 2014-1-8;

package com.android.server.bigbird;

import android.util.Log;
import android.content.Context;
import android.fakeled.IFakeLed;

public class FakeLedService extends IFakeLed.Stub{
	private static final boolean DEBUG = true;
	private static final String TAG = "FakeLedService";
	private final Context mContext;
	private final Object mLock = new Object();

	public FakeLedService(Context context){
		mContext = context;		
	}

	public int createTest(){
		synchronized(mLock){
			if(DEBUG){
				Log.d(TAG, "--->createTest!");
			}
		}
		return 0;
	}
	public int setLedFlashEnable(int enable){
		synchronized(mLock){
			if(DEBUG){
				Log.d(TAG, "--->setLedFlashEnable!");
			}
		}
		return 0;
	}
	public int getLedFlashEnable(){
		synchronized(mLock){
			if(DEBUG){
				Log.d(TAG, "--->getLedFlashEnable!");
			}
		}
		return 0;
	}
}
10.vi frameworks/base/services/java/com/android/server/SystemServer.java
diff --git a/frameworks/base/services/java/com/android/server/SystemServer.java b/frameworks/base/services/java/com/android/server/SystemServer.java
index 0e0f156..6e06b46 100644
--- a/frameworks/base/services/java/com/android/server/SystemServer.java
+++ b/frameworks/base/services/java/com/android/server/SystemServer.java
@@ -68,6 +68,7 @@ import com.android.server.search.SearchManagerService;
 import com.android.server.usb.UsbService;
 import com.android.server.wifi.WifiService;
 import com.android.server.wm.WindowManagerService;
+import com.android.server.bigbird.FakeLedService;
 import dalvik.system.VMRuntime;
 import dalvik.system.Zygote;
@@ -208,6 +209,9 @@ class ServerThread {
         boolean disableNetwork = SystemProperties.getBoolean("config.disable_network", false);
         try {
+ Slog.i(TAG, "--->add FakeLedService");
+ ServiceManager.addService("bigbirdfakeled", new FakeLedService(context));
+
             Slog.i(TAG, "Display Manager");
             display = new DisplayManagerService(context, wmHandler);
             ServiceManager.addService(Context.DISPLAY_SERVICE, display, true);
11.vi frameworks/base/core/java/android/app/ContextImpl.java
diff --git a/frameworks/base/core/java/android/app/ContextImpl.java b/frameworks/base/core/java/android/app/ContextImpl.java
index 190ddb4..b5d8c2b 100644
--- a/frameworks/base/core/java/android/app/ContextImpl.java
+++ b/frameworks/base/core/java/android/app/ContextImpl.java
@@ -118,6 +118,9 @@ import com.android.internal.annotations.GuardedBy;
 import com.android.internal.app.IAppOpsService;
 import com.android.internal.os.IDropBoxManagerService;
+import android.fakeled.IFakeLed;
+import android.fakeled.FakeLedManager;
+
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -306,6 +309,14 @@ class ContextImpl extends Context {
             }};
     static {
+ registerService("FakeLedManager", new ServiceFetcher(){
+ public Object createService(ContextImpl ctx){
+ IBinder b = ServiceManager.getService("bigbirdfakeled");
+ IFakeLed service = IFakeLed.Stub.asInterface(b);
+ return new FakeLedManager(ctx,service);
+ }
+ });
+
         registerService(ACCESSIBILITY_SERVICE, new ServiceFetcher() {
                 public Object getService(ContextImpl ctx) {
                     return AccessibilityManager.getInstance(ctx);
12.make    --->compile ok;
13.out/host/linux-x86/bin/emulator
#service list | grep FakeLed
61 bigbirdfakeled: [android.fakeled.IFakeLed]
14.打包jar文件给应用(使用eclipse):
打包:FakeLedManager/IFakeLed.aidl和gen/IFakeLed.java;
15.应用中调用方法(导入上步骤中jar包):
FakeLedManager fakeLed = (FakeLedManager)getSystemService("FakeLedManager");
        FakeLedManager fakeLed = (FakeLedManager)getSystemService("FakeLedManager");
        fakeLed.createTest();
        fakeLed.getLedFlashEnable();
        fakeLed.setLedFlashEnable(1);

你可能感兴趣的:(android_添加服务给应用使用)