8.1中添加系统service与之前有差异,涉及到te文件。网上找了些例子没有实现。特记录总结!

1.frameworks/base 目录下添加对应的文件清单如下
frameworks/base/core/java/android/app/HelloWorldManager.java
frameworks/base/core/java/android/app/IHelloWorldManager.aidl
frameworks/base/services/core/java/com/android/server/HelloWorldService.java
很好理解 一个service 一个manager 一个aidl文件
功能很简单就打印 Hello,World!
再修改
frameworks/base/core/java/android/app/SystemServiceRegistry.java
frameworks/base/core/java/android/content/Context.java
frameworks/base/services/java/com/android/server/SystemServer.java
很显然是为了添加及注册系统级service "hello"
注意修改完成后使用make update-api
特别提醒如果此步骤中 make update-api 不成功一定要修改成编译成功
make update-api 成功后会修改几个txt文件
frameworks/base/Android.mk
frameworks/base/api/current.txt
frameworks/base/api/system-current.txt
frameworks/base/api/test-current.txt


记得在修改SystemServiceRegistry.java Context.java SystemServer.java 参考Vibrator添加


SystemServiceRegistry.java
registerService(Context.HELLO_SERVICE, HelloWorldManager.class,
                new CachedServiceFetcher() {
            @Override
            public HelloWorldManager createService(ContextImpl ctx) throws ServiceNotFoundException {
                  IBinder binder;
                if (true){//ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
                    binder = ServiceManager.getServiceOrThrow(Context.HELLO_SERVICE);
                } else {
                    binder = ServiceManager.getService(Context.HELLO_SERVICE);
                }
                return new HelloWorldManager(ctx,IHelloWorldManager.Stub.asInterface(binder));
            }});
 


a/frameworks/base/core/java/android/content/Context.java b/frameworks/base/core/java/android/content/
old mode 100644
new mode 100755
index df351ba..584a8c4
--- a/frameworks/base/core/java/android/content/Context.java
+++ b/frameworks/base/core/java/android/content/Context.java
@@ -2911,6 +2911,7 @@ public abstract class Context {
             ACCOUNT_SERVICE,
             ACTIVITY_SERVICE,
             ALARM_SERVICE,
+            HELLO_SERVICE,
             NOTIFICATION_SERVICE,
             ACCESSIBILITY_SERVICE,
             CAPTIONING_SERVICE,
@@ -3018,6 +3019,9 @@ public abstract class Context {
      * 
{@link #ALARM_SERVICE} ("alarm")
      * 
A {@link android.app.AlarmManager} for receiving intents at the
      *  time of your choosing.
+     * 
{@link #HELLO_SERVICE} ("hello")
+     * 
A {@link android.app.HelloWorldManager} for receiving intents at the
+     *  time of your choosing.
      * 
{@link #NOTIFICATION_SERVICE} ("notification")
      * 
A {@link android.app.NotificationManager} for informing the user
      *   of background events.
@@ -3082,6 +3086,8 @@ public abstract class Context {
      * @see android.os.PowerManager
      * @see #ALARM_SERVICE
      * @see android.app.AlarmManager
+     * @see #HELLO_SERVICE
+     * @see android.app.HelloWorldManager
      * @see #NOTIFICATION_SERVICE
      * @see android.app.NotificationManager
      * @see #KEYGUARD_SERVICE
@@ -3133,7 +3139,8 @@ public abstract class Context {
      * Currently available classes are:
      * {@link android.view.WindowManager}, {@link android.view.LayoutInflater},
      * {@link android.app.ActivityManager}, {@link android.os.PowerManager},
-     * {@link android.app.AlarmManager}, {@link android.app.NotificationManager},
+     * {@link android.app.AlarmManager},
+     * {@link android.app.HelloWorldManager}, {@link android.app.NotificationManager},
      * {@link android.app.KeyguardManager}, {@link android.location.LocationManager},
      * {@link android.app.SearchManager}, {@link android.os.Vibrator},
      * {@link android.net.ConnectivityManager},
@@ -3238,6 +3245,16 @@ public abstract class Context {
      * @see android.app.AlarmManager
      */
     public static final String ALARM_SERVICE = "alarm";
+    
+    /**
+     * Use with {@link #getSystemService} to retrieve a
+     * {@link android.app.HelloWorldManager} for receiving intents at a
+     * time of your choosing.
+     *
+     * @see #getSystemService
+     * @see android.app.HelloWorldManager
+     */
+    public static final String HELLO_SERVICE = "hello"; 




a/frameworks/base/services/java/com/android/server/SystemServer.java
+++ b/frameworks/base/services/java/com/android/server/SystemServer.java
@@ -140,6 +140,7 @@ import static android.view.Display.DEFAULT_DISPLAY;
 import java.lang.reflect.Constructor;
 import android.os.IInterface;
 import java.lang.reflect.InvocationTargetException;
+import com.android.server.HelloWorldService;
 //import com.mediatek.fullscreenswitch.FullscreenSwitchService;
 /// @}
 
@@ -175,6 +176,8 @@ public final class SystemServer {
             "com.android.server.voiceinteraction.VoiceInteractionManagerService";
     private static final String PRINT_MANAGER_SERVICE_CLASS =
             "com.android.server.print.PrintManagerService";
+    private static final String Hello_MANAGER_SERVICE_CLASS =
+            "com.android.server.HelloWorldService";
     private static final String COMPANION_DEVICE_MANAGER_SERVICE_CLASS =
             "com.android.server.companion.CompanionDeviceManagerService";
     private static final String USB_SERVICE_CLASS =
@@ -713,6 +716,7 @@ public final class SystemServer {
         NsdService serviceDiscovery= null;
         WindowManagerService wm = null;
         SerialService serial = null;
+        HelloWorldService hello = null;
         NetworkTimeUpdateService networkTimeUpdater = null;
         CommonTimeManagementService commonTimeMgmtService = null;
         InputManagerService inputManager = null;
@@ -1357,6 +1361,19 @@ public final class SystemServer {
                     traceEnd();
                 }
 
+                   if (true) {
+                    traceBeginAndSlog("StartHelloService");
+                    try {
+                        // Serial port support
+                        hello = new HelloWorldService(context);
+                        ServiceManager.addService(Context.HELLO_SERVICE, hello);
+                    } catch (Throwable e) {
+                        Slog.e(TAG, "Failure starting StartHelloService", e);
+                    }
+                    traceEnd();
+                }

+

frameworks/base/core/java/android/app/HelloWorldManager.java

package android.app;import android.os.RemoteException;import android.annotation.SystemService;import android.content.Context;@SystemService(Context.HELLO_SERVICE)public final class HelloWorldManager{  private final IHelloWorldManager mService;    private Context mContext;  /**    * @hide to prevent subclassing from outside of the framework   */  HelloWorldManager(Context context,IHelloWorldManager service){        mContext = context;    mService = service;  }  /**    * Like {@link #HelloWorldManager( )}, but allowing the caller to specify   * that the vibration is owned by someone else.   * @hide   */  public void printHello(){    try{      mService.printHello();    }catch (RemoteException ex){    }     }}

frameworks/base/core/java/android/app/IHelloWorldManager.aidl

package android.app;/*** System private API for talking with the helloworld service.* {@hide}*/interface IHelloWorldManager{   void printHello();}

frameworks/base/services/core/java/com/android/server/HelloWorldService.java

package com.android.server;import android.app.IHelloWorldManager;import android.content.Context;import android.os.RemoteException;import android.util.Slog;import android.content.Context;import com.android.server.SystemService;public class HelloWorldService extends IHelloWorldManager.Stub {    private final static String LOG_TAG = "HelloWorldService";    private static final int BACKGROUND_USER_ID = -10;    private final Object mLock = new Object();    private final Context mContext;        HelloWorldService(Context context) {        mContext = context;    }       @Override    public void printHello() throws RemoteException {        Slog.i(LOG_TAG,"xuyong Hello,World!");    }   }

当然不要忘了frameworks/base/Android.mk 中添加aidl  core/java/android/app/IHelloWorldManager.aidl \

如果上述操作做完编译后无误的话至少hello service现在是在系统中的

在添加te文件时有两种思路 一种是按照system/sepolicy 目录中添加vibrator service的步骤添加hello service

另一种是在device 目录下添加hello.te 文件等操作实现。下面先说简单的:在system/sepolicy 目录中添加

system/sepolicy/prebuilts/api/26.0/nonplat_sepolicy.cil
system/sepolicy/prebuilts/api/26.0/private/service_contexts
system/sepolicy/prebuilts/api/26.0/public/service.te
system/sepolicy/private/compat/26.0/26.0.cil
system/sepolicy/private/service_contexts

system/sepolicy/public/service.te

此步骤很简单 就完整照Vibrator 添加 注意一点 :hello" 对应 Context 中的HELLO_SERVICE

system/sepolicy/private/service_contexts

hello                                     u:object_r:hello_service:s0

system/sepolicy/prebuilts/api/26.0/public/service.te

type hello_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;

文件修改后可以mmm system/sepolicy/ 验证语法或规则是否符合要求。

下面介绍另一种添加te文件方式 我们是以sprd的代码为例

device/sprd/sharkl2/common/plat_sepolicy/private/service_contexts
device/sprd/sharkl2/common/plat_sepolicy/private/system_server.te
device/sprd/sharkl2/common/plat_sepolicy/public/service.te

device/sprd/sharkl2/common/sepolicy/system_server.te

device/sprd/sharkl2/common/plat_sepolicy/private/service_contexts

hello                               u:object_r:hello_service:s0

device/sprd/sharkl2/common/plat_sepolicy/private/system_server.te添加

allow system_server hello_service:service_manager { add };

device/sprd/sharkl2/common/plat_sepolicy/public/service.te添加

type hello_service,             service_manager_type;

device/sprd/sharkl2/common/sepolicy/system_server.te添加

allow system_server hello_service:service_manager add;

添加两个文件

device/sprd/sharkl2/common/plat_sepolicy/private/hello.te

typeattribute hello coredomain;
init_daemon_domain(hello)

device/sprd/sharkl2/common/plat_sepolicy/public/hello.te

type hello, domain;

type hello_exec, exec_type, file_type;

文件修改后可以mmm system/sepolicy/ 验证语法或规则是否符合要求.

2.完整编译 刷机 开机后 使用adb shell 进入后 手机  service list | grep hello 如果能显示说明系统中已添加成功hello service

3.编写测试程序 使用hello service 我是在Settings 中添加的

private void callHelloService(){Log.d("xuyong","onclick begin");HelloWorldManager hello = ((HelloWorldManager)mContext.getSystemService(Context.HELLO_SERVICE));if (hello != null){Log.d("xuyong","callHelloService successful!");hello.printHello();}Log.d("xuyong","onclick end");} 

记得import android.app.HelloWorldManager;

最后上传源码:

下载源码

https://download.csdn.net/download/yong_xu/10519366

更多相关文章

  1. [转]Android 技术专题系列之九 -- 图形系统
  2. Android 的GUI 系统
  3. Android系统信息与安全机制
  4. Android调用系统分享,资源未找到问题
  5. android系统权限关机重启
  6. Android模拟、实现、触发系统按键事件的方法
  7. android 日期时间格式转换;软键盘显示消失;获取系统title

随机推荐

  1. List集合封装获取参数
  2. 0318作业
  3. 面向对象编程基础
  4. 如何充分利用小程序?
  5. 通过注解的 springboot+mybatis 多数据
  6. 0331作业
  7. iview的table合并相同的单元格
  8. 经济下行,程序员年底升职加薪指南
  9. 3.19作业
  10. webuploader文件上传到哪里的问题