最简System Daemon Service

---
 Android.mk                                    |  1 +
 core/java/android/app/ContextImpl.java        |  9 +++++
 core/java/android/os/IMyServiceManager.aidl   |  9 +++++
 core/java/android/os/MyServiceManager.java    | 25 +++++++++++++
 .../java/com/android/server/MyService.java    | 36 +++++++++++++++++++
 .../java/com/android/server/SystemServer.java |  7 ++++
 .../server/am/ActivityManagerService.java     |  3 ++
 7 files changed, 90 insertions(+)
 create mode 100644 core/java/android/os/IMyServiceManager.aidl
 create mode 100644 core/java/android/os/MyServiceManager.java
 create mode 100644 services/java/com/android/server/MyService.java

diff --git a/Android.mk b/Android.mk
index 7406f7298e9..8c65e17b997 100755
--- a/Android.mk
+++ b/Android.mk
@@ -181,6 +181,7 @@ LOCAL_SRC_FILES += \
    core/java/android/os/IUserManager.aidl \
    core/java/android/os/display/IDisplayService.aidl \
    core/java/android/os/IVibratorService.aidl \
+   core/java/android/os/IMyServiceManager.aidl \
    core/java/android/service/notification/INotificationListener.aidl \
    core/java/android/print/ILayoutResultCallback.aidl \
    core/java/android/print/IPrinterDiscoveryObserver.aidl \
diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java
index a1c454164a7..8569d3f8583 100644
--- a/core/java/android/app/ContextImpl.java
+++ b/core/java/android/app/ContextImpl.java
@@ -110,6 +110,8 @@ import android.os.storage.IMountService;
 import android.os.storage.StorageManager;
 import android.os.display.IDisplayService;
 import android.os.SystemProperties;
+import android.os.MyServiceManager;
+import android.os.IMyServiceManager;
 import android.print.IPrintManager;
 import android.print.PrintManager;
 import android.telephony.TelephonyManager;
@@ -424,6 +426,13 @@ class ContextImpl extends Context {
                         ctx.getPackageName());
                 }});
 
+        registerService("MYSERVICE", new ServiceFetcher() {
+            public Object createService(ContextImpl ctx) {
+                IBinder b = ServiceManager.getService("MYSERVICE");
+                return new MyServiceManager(IMyServiceManager.Stub.asInterface(b));
+            }});
+
+
         registerService(COUNTRY_DETECTOR, new StaticServiceFetcher() {
                 public Object createStaticService() {
                     IBinder b = ServiceManager.getService(COUNTRY_DETECTOR);
diff --git a/core/java/android/os/IMyServiceManager.aidl b/core/java/android/os/IMyServiceManager.aidl
new file mode 100644
index 00000000000..745b9962c64
--- /dev/null
+++ b/core/java/android/os/IMyServiceManager.aidl
@@ -0,0 +1,9 @@
+package android.os;
+
+
+interface IMyServiceManager {
+
+    void run();
+
+
+}
diff --git a/core/java/android/os/MyServiceManager.java b/core/java/android/os/MyServiceManager.java
new file mode 100644
index 00000000000..26bd9775c9d
--- /dev/null
+++ b/core/java/android/os/MyServiceManager.java
@@ -0,0 +1,25 @@
+package android.os;
+
+import static com.android.internal.util.Preconditions.checkNotNull;
+import android.util.Log;
+
+
+public class MyServiceManager {
+    private final String TAG = "MyServiceManager";
+    private final IMyServiceManager mService;
+
+    public MyServiceManager(IMyServiceManager service) {
+        mService = checkNotNull(service, "missing IMyServiceManager");
+    }
+
+    public void run() {
+        try {
+            mService.run();
+        } catch (RemoteException e) {
+            Log.d(TAG, "wtf !!!");
+        }
+    }
+
+
+
+}
diff --git a/services/java/com/android/server/MyService.java b/services/java/com/android/server/MyService.java
new file mode 100644
index 00000000000..7ec9378cc38
--- /dev/null
+++ b/services/java/com/android/server/MyService.java
@@ -0,0 +1,36 @@
+package com.android.server;
+
+import android.os.IMyServiceManager;
+import android.util.Log;
+
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
+import java.util.Timer;
+import java.util.TimerTask;
+
+
+public class MyService extends IMyServiceManager.Stub {
+
+    private final static String TAG = "MyService";
+    private int refCount = 0;
+
+    @Override
+    public void run() {
+
+        Timer timer = new Timer();
+        timer.schedule(new TimerTask() {
+            @Override
+            public void run() {
+                refCount ++;
+                Log.d(TAG, "MyService runs " + refCount + " times!");
+            }
+        }, 1000, 1000);
+    }
+
+    @Override
+    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
+        pw.println("MyService runs for times :");
+        pw.println(refCount);
+    }
+}
+
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index fcb36ee8ab9..f93a1d14b6b 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -1426,6 +1426,13 @@ class NetworkThread extends Thread {
             } catch (Throwable e) {
                 reportWtf("starting Service Discovery Service", e);
             }//}
+
+        try{
+            Slog.i(TAG, "Start add MyService");
+            ServiceManager.addService("MYSERVICE", new MyService());
+        } catch (Throwable e) {
+            Slog.e(TAG, "Failure starting My Service", e);
+        }
             android.os.Process.setThreadPriority(
                         android.os.Process.THREAD_PRIORITY_FOREGROUND);
 
diff --git a/services/java/com/android/server/am/ActivityManagerService.java b/services/java/com/android/server/am/ActivityManagerService.java
index ccfa379b505..3933ac82adf 100644
--- a/services/java/com/android/server/am/ActivityManagerService.java
+++ b/services/java/com/android/server/am/ActivityManagerService.java
@@ -156,6 +156,7 @@ import android.os.SystemClock;
 import android.os.SystemProperties;
 import android.os.UpdateLock;
 import android.os.UserHandle;
+import android.os.MyServiceManager;
 import android.provider.Settings;
 import android.text.format.DateUtils;
 import android.text.format.Time;
@@ -10185,6 +10186,8 @@ public final class ActivityManagerService extends ActivityManagerNative
             mAppOpsService.reload();
             mStackSupervisor.resumeTopActivitiesLocked();
             sendUserSwitchBroadcastsLocked(-1, mCurrentUserId);
+            MyServiceManager myService = (MyServiceManager) mContext.getSystemService("MYSERVICE");
+            myService.run();
         }
 
     }
-- 
2.18.0

你可能感兴趣的:(最简System Daemon Service)